/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.scene.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.constants.DelFlag;
import com.lczyfz.istep.common.constants.SceneConstants;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.CheckUtils;
import com.lczyfz.istep.common.utils.IdGen;
import com.lczyfz.istep.common.utils.JsonModel;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.openstack.api.nova.impl.VirtualMachineImpl;
import com.lczyfz.istep.modules.openstack.api.nova.impl.serveraction.ConsolePartImpl;
import com.lczyfz.istep.modules.openstack.api.nova.impl.serveraction.ServerActionsImpl;
import com.lczyfz.istep.modules.openstack.api.nova.model.Server;
import com.lczyfz.istep.modules.openstack.api.nova.model.serveraction.ConsoleIndex;
import com.lczyfz.istep.modules.openstack.api.nova.model.serveraction.ConsoleType;
import com.lczyfz.istep.modules.resource.entity.ResourceCourseware;
import com.lczyfz.istep.modules.resource.entity.ResourceQuestion;
import com.lczyfz.istep.modules.resource.service.ResourceCoursewareService;
import com.lczyfz.istep.modules.resource.service.ResourceQuestionService;
import com.lczyfz.istep.modules.scene.entity.*;
import com.lczyfz.istep.modules.scene.service.*;
import com.lczyfz.istep.modules.sys.entity.ScheduleJob;
import com.lczyfz.istep.modules.sys.entity.User;
import com.lczyfz.istep.modules.sys.service.ScheduleJobService;
import com.lczyfz.istep.modules.sys.service.SystemService;
import com.lczyfz.istep.modules.sys.utils.UserUtils;
import org.activiti.engine.impl.util.json.JSONArray;
import org.activiti.engine.impl.util.json.JSONObject;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

/**
 * 教学实验环境Controller
 * @author dingh
 * @version 2017-03-07
 */
@Controller
@RequestMapping(value = "${adminPath}/scene/sceneEnvironment")
public class SceneEnvironmentController extends BaseController {

	@Autowired
	private SceneEnvironmentService sceneEnvironmentService;
	@Autowired
	private SceneNetworkService sceneNetworkService;
	@Autowired
	private SceneRouterService sceneRouterService;
	@Autowired
	private SceneSecurityGroupService sceneSecurityGroupService;
	@Autowired
	private SceneSecurityGroupRuleService sceneSecurityGroupRuleService;
	@Autowired
	private SceneSubnetService sceneSubnetService;
	@Autowired
	private SceneVmTemplateService sceneVmTemplateService;
	@Autowired
	private SceneVmTypeService sceneVmTypeService;
	@Autowired
	private ScenePortService scenePortService;
	@Autowired
	private SceneEnvironmentInstanceService sceneEnvironmentInstanceService;
	@Autowired
	private ResourceCoursewareService resourceCoursewareService;
	@Autowired
	private ResourceQuestionService resourceQuestionService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private SystemService SceneVmTypeService;
	@Autowired
	private ScheduleJobService scheduleJobService;
	@ModelAttribute
	public SceneEnvironment get(@RequestParam(required=false) String id) {
		SceneEnvironment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sceneEnvironmentService.get(id);
		}
		if (entity == null){
			entity = new SceneEnvironment();
		}
		return entity;
	}

	@RequiresPermissions("scene:sceneEnvironment:view")
	@RequestMapping(value = {"list", ""})
	public String list(SceneEnvironment sceneEnvironment, HttpServletRequest request, HttpServletResponse response, Model model) {

		if(systemService.isSuperAdmin())
		{
			model.addAttribute("isSuperAdmin", "true");

		}
		else
		{
			model.addAttribute("isSuperAdmin", "false");
		}
		Page<SceneEnvironment> page = sceneEnvironmentService.findPage(new Page<SceneEnvironment>(request, response), sceneEnvironment);
		model.addAttribute("page", page);
		logger.debug("dingh===12345");


		return "modules/scene/sceneEnvironmentList";
	}

	@RequiresPermissions("scene:sceneEnvironment:view")
	@RequestMapping(value = "form")
	public String form(SceneEnvironment sceneEnvironment, Model model) {
		if(systemService.isSuperAdmin())
		{
			logger.debug("dingh===true");
			model.addAttribute("isSuperAdmin", "true");
		}else {
			model.addAttribute("isSuperAdmin", "false");
			logger.debug("dingh===false");
		}


		model.addAttribute("sceneEnvironment", sceneEnvironment);
		return "modules/scene/sceneEnvironmentForm";
	}

	@RequiresPermissions("scene:sceneEnvironment:edit")
	@RequestMapping(value = "save")
	public String save(SceneEnvironment sceneEnvironment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sceneEnvironment)){
			return form(sceneEnvironment, model);
		}

		//拓扑保存-网络
		SceneNetwork sceneNetwork=new SceneNetwork();
		sceneNetwork.setNetworkName("net2");
//		sceneNetworkService.save(sceneNetwork);

		//保存子网
		SceneSubnet subnet=new SceneSubnet();
		subnet.setSubnetName("snet2");
		subnet.setNetworkId(sceneNetwork.getId());
		subnet.setIpVersion("4");
		subnet.setSubnetpoolId("1210b7c9-4ef5-482a-954c-a510e39ef77c");
//		sceneSubnetService.save(subnet);

		//保存路由信息
		SceneRouter sceneRouter=new SceneRouter();
		sceneRouter.setRouterName("router2");
		sceneRouter.setNetworkId("0f101954-e971-4f91-b03a-0f2533c19cbf");
//		sceneRouterService.save(sceneRouter);
		//增加接口
//		sceneRouterService.saveInterface(sceneRouter.getId(), subnet.getId());


		//安全组
		SceneSecurityGroup sceneSecurityGrou=new SceneSecurityGroup();
		sceneSecurityGrou.setName("sg2");
		sceneSecurityGrou.setRemarks("2");
//		sceneSecurityGroupService.save(sceneSecurityGrou);

		//规则
		SceneSecurityGroupRule sceneSecurityGroupRule=new SceneSecurityGroupRule();
		sceneSecurityGroupRule.setDirection("egress");
		sceneSecurityGroupRule.setEthertype("IPv4");
		sceneSecurityGroupRule.setProtocol("tcp");
		sceneSecurityGroupRule.setSecurityGroupId(sceneSecurityGrou.getId());
		sceneSecurityGroupRule.setRemoteGroupId("b732670c-d01e-40c3-bb4e-1ae6c6ec2e5c");
		sceneSecurityGroupRule.setFromPort("8080");
		sceneSecurityGroupRule.setToPort("9090");
//		sceneSecurityGroupRuleService.save(sceneSecurityGroupRule);

		//虚拟机b22c8260-f4ef-4010-83bb-317c3cf911e0
		SceneVmTemplate sceneVmTemplate=new SceneVmTemplate();
		sceneVmTemplate.setName("vm2");
		sceneVmTemplate.setImageRef("a6512f9d-b9ee-44d5-8fb3-e68e368c0b0a");

//		sceneVmTemplateService.save(sceneVmTemplate);

//		sceneVmTemplateService.vmNetworkInsert(sceneVmTemplate.getId(), sceneNetwork.getId());

		sceneEnvironmentService.save(sceneEnvironment);
		logger.debug("evmSgInsert===" + sceneEnvironment.getId()+" "+sceneSecurityGrou.getId());


//		sceneEnvironmentService.evmSgInsert(sceneEnvironment.getId(), sceneSecurityGrou.getId());
//		sceneEnvironmentService.evmNetworkInsert(sceneEnvironment.getId(), sceneNetwork.getId());
//		sceneEnvironmentService.evmRouterInsert(sceneEnvironment.getId(),sceneRouter.getId());
//		sceneEnvironmentService.evmNetworkInsert(sceneEnvironment.getId(),sceneVmTemplate.getId());


		addMessage(redirectAttributes, "保存教学实验环境成功");
		return "redirect:"+Global.getAdminPath()+"/scene/sceneEnvironment/?repage";
	}

	@RequiresPermissions("scene:sceneEnvironment:edit")
	@RequestMapping(value = "delete")
	public String delete(SceneEnvironment sceneEnvironment, RedirectAttributes redirectAttributes) throws RuntimeException,Exception{
		sceneEnvironmentService.delete(sceneEnvironment);
		sceneEnvironmentService.evmRelateDelete(sceneEnvironment);
		addMessage(redirectAttributes, "删除教学实验环境成功");
		return "redirect:"+Global.getAdminPath()+"/scene/sceneEnvironment/?repage";
	}
	@RequiresPermissions("scene:sceneEnvironment:view")
	@RequestMapping(value = "topology")
	public String topology(SceneEnvironment sceneEnvironment, Model model, RedirectAttributes redirectAttributes) {


		SceneEnvironment temp=sceneEnvironmentService.findVmTemplateByName(sceneEnvironment.getEnvironmentName(),DelFlag.DEL_FLAG_NORMAL);
		if(sceneEnvironmentService.get(sceneEnvironment)==null)
		{
			if(temp!=null)
			{
				addMessage(redirectAttributes, "保存教学实验环境失败，该实验环境已存在");
				return "redirect:"+Global.getAdminPath()+"/scene/sceneEnvironment/?repage";
			}
		}else {
			if(temp!=null&&!temp.getId().equals(sceneEnvironment.getId()))
			{
				addMessage(redirectAttributes, "修改教学实验环境失败，该实验环境已存在");
				return "redirect:"+Global.getAdminPath()+"/scene/sceneEnvironment/?repage";
			}
		}

		if(sceneEnvironment.getIncreaseTimeAllow().equals("1"))
		{
			sceneEnvironment.setIncreaseNumber(null);
			sceneEnvironment.setIncreaseTime(null);
		}
		model.addAttribute("sceneEnvironment", sceneEnvironment);
		logger.debug("s===111" + sceneEnvironment.getEnvironmentName());
		logger.debug("saveTopology===111" + sceneEnvironment.getTopologyXml());
		if(sceneEnvironment.getTopologyXml()!=null&&!sceneEnvironment.getTopologyXml().equals("")) {
			String str = sceneEnvironment.getTopologyXml().replace("\"", "\\\"");
			sceneEnvironment.setTopologyXml(str);
		}

		logger.debug("saveTopology===1113" + sceneEnvironment.getTopologyXml());

		return "modules/scene/sceneEnvironmentTopology";
	}

	/**
	 * 返回信息列表
	 */
	@ResponseBody
	@RequestMapping(value = "envInfo")
	public JsonModel envInfo() {
		JsonModel jsonModel = new JsonModel();
		Map<String,List> map = new HashMap<>();
		List<SceneRouter> publicNetworkList=new ArrayList<>();
		List<String> publicNetworkAttri=new ArrayList<>();

//		List<SceneRouter> sceneRouterList=sceneRouterService.findList(new SceneRouter());
//		List<String> routerAttri=new ArrayList<>();

//		List<SceneNetwork> sceneNetworkList=sceneNetworkService.findList(new SceneNetwork());
//		List<String> networkAttri=new ArrayList<>();

		List<SceneSecurityGroup> sceneSecurityGroupList=sceneSecurityGroupService.findList(new SceneSecurityGroup());
		List<String> securityGroupAttri=new ArrayList<>();


		List<SceneVmTemplate> sceneVmTemplateList=sceneVmTemplateService.findList(new SceneVmTemplate());
		List<String> vmTemplateAttri=new ArrayList<>();



//		try{
//			NetworksImpl networkdemo = new NetworksImpl();
//			List<Network> networks = networkdemo.getNetwork();
//			for(Network network : networks){
//				if(network.getName().equals("provider"))
//				{
//					logger.debug("publicNetworkAttri===111");
//					publicNetworkAttri.add("{\"desc\":" + "\"" +network.getName()+ "\"" + ",\"value\":" + "\"" + network.getId()+ "\"}");
//
////					publicNetworkAttri.add("1=" + network.getId() + "&&" + network.getName());
//					break;
//				}
//			}
//
//		}catch (RuntimeException e){
//
//		}
//		publicNetworkAttri.add("1=0f101954-e971-4f91-b03a-0f2533c19cb&&public");

//		logger.debug("saveTopology===dg" + sceneRouterList.size());
//
//		for(int i=0;i<sceneRouterList.size();i++)
//		{
//			if(i==0)
//				routerAttri.add("1=" + sceneRouterList.get(i).getId() + "&&" + sceneRouterList.get(i).getRouterName());
//			else
//				routerAttri.add("0=" + sceneRouterList.get(i).getId() + "&&" + sceneRouterList.get(i).getRouterName());
//		}

//		for(int i=0;i<sceneNetworkList.size();i++)
//		{
//			if(i==0)
//				networkAttri.add("1=" + sceneNetworkList.get(i).getId() + "&&" + sceneNetworkList.get(i).getNetworkName());
//			else
//				networkAttri.add("0=" + sceneNetworkList.get(i).getId() + "&&" + sceneNetworkList.get(i).getNetworkName());
//		}

		for(int i=0;i<sceneSecurityGroupList.size();i++)
		{
			securityGroupAttri.add("{\"desc\":"+"\""+sceneSecurityGroupList.get(i).getName()+"\""+",\"value\":"+"\""+sceneSecurityGroupList.get(i).getId()+"\"}");

//			if(i==0)
//				securityGroupAttri.add("1=" + sceneSecurityGroupList.get(i).getId() + "&&" + sceneSecurityGroupList.get(i).getName());
//			else
//				securityGroupAttri.add("0=" + sceneSecurityGroupList.get(i).getId() + "&&" + sceneSecurityGroupList.get(i).getName());
		}

		for(int i=0;i<sceneVmTemplateList.size();i++)
		{
			vmTemplateAttri.add("{\"desc\":"+"\""+sceneVmTemplateList.get(i).getName()+"\""+",\"value\":"+"\""+sceneVmTemplateList.get(i).getId()+"\"}");

//			if(i==0)
//				vmTemplateAttri.add("1=" + sceneVmTemplateList.get(i).getId() + "&&" + sceneVmTemplateList.get(i).getName());
//			else
//				vmTemplateAttri.add("0=" + sceneVmTemplateList.get(i).getId() + "&&" + sceneVmTemplateList.get(i).getName());
		}


//		map.put("routerList", sceneRouterService.findList(new SceneRouter()));
//		map.put("networkList", sceneNetworkService.findList(new SceneNetwork()));
//		map.put("securityGroupList", sceneSecurityGroupService.findList(new SceneSecurityGroup()));
//		map.put("vmTemplateList",sceneVmTemplateService.findList(new SceneVmTemplate()));
		map.put("publicNetworkAttri", publicNetworkAttri);
//		map.put("routerAttri", routerAttri);
//		map.put("networkAttri", networkAttri);
		map.put("securityGroupAttri",securityGroupAttri);
		map.put("vmTemplateAttri",vmTemplateAttri);



		jsonModel.setDataObj(map);
		return jsonModel;
	}
	@ResponseBody
	@RequestMapping(value = "envInfo1")
	public JsonModel envInfo1() {
		JsonModel jsonModel = new JsonModel();
		Map<String,List> map = new HashMap<>();

		List<SceneVmTemplate> sceneVmTemplateList=sceneVmTemplateService.findList(new SceneVmTemplate());
		List<String> vmTemplateAttri=new ArrayList<>();

		for(int i=0;i<sceneVmTemplateList.size();i++)
		{
			vmTemplateAttri.add("{\"desc\":"+"\""+sceneVmTemplateList.get(i).getName()+"\""+",\"value\":"+"\""+sceneVmTemplateList.get(i).getId()+"\"}");
//			if(i==0)
//				vmTemplateAttri.add("1=" + sceneVmTemplateList.get(i).getId() + "&&" + sceneVmTemplateList.get(i).getName());
//			else
//				vmTemplateAttri.add("0=" + sceneVmTemplateList.get(i).getId() + "&&" + sceneVmTemplateList.get(i).getName());
		}
		//{"title":"IP版本","type":"select","data":[{"desc":"IPv4","value":"0"},{"desc":"IPv6","value":"1"}]}
		map.put("vmTemplateAttri",vmTemplateAttri);
		logger.debug("dingh===111111");
		logger.debug("dingh==="+vmTemplateAttri);

		for(int i=0;i<vmTemplateAttri.size();i++)
		{
			logger.debug("dingh==="+vmTemplateAttri.get(i));
		}

		jsonModel.setDataObj(map);
		return jsonModel;
	}

	/**
	 * 创建实例
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "createInstance")
	public String createInstance(@RequestParam(required = false) String ids) throws Exception{
//		SceneEnvironmentInstance sceneEnvironmentInstance=new SceneEnvironmentInstance();
//		List<SceneVmTemplate> vmUrlList=new ArrayList<>();
//		sceneEnvironmentService.createInstance(sceneEnvironment, sceneEnvironmentInstance, vmUrlList);


		logger.debug("createInstance==="+ids);

		SceneEnvironmentInstance sceneEnvironmentInstance=new SceneEnvironmentInstance();
		List<SceneVmTemplate> vmUrlList=new ArrayList<>();
		SceneEnvironment sceneEnvironment=sceneEnvironmentService.get(ids);
		sceneEnvironmentInstance.setAutoDelete(SceneConstants.AUTO_DELETE_YES);
		sceneEnvironmentService.createInstance(sceneEnvironment, sceneEnvironmentInstance, vmUrlList);

////		logger.debug("dingh===定时任务开始");
////		sceneEnvironmentService.instanceScheduleJob(sceneEnvironmentInstance,sceneEnvironment);
//		model.addAttribute("vmUrlList", vmUrlList);
//
//		addMessage(redirectAttributes, "生成实例成功");
//		return "redirect:"+Global.getAdminPath()+"/scene/sceneEnvironmentInstance";
		return null;
	}
	/**
	 * 创建实例
	 * @return
	 */
	@RequiresPermissions("scene:sceneEnvironment:edit")
	@RequestMapping(value = "toSceneEnvironmentInstance")
	public String toSceneEnvironmentInstance(SceneEnvironment sceneEnvironment, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, sceneEnvironment)){
			return form(sceneEnvironment, model);
		}
		logger.debug("toSceneEnvironmentInstance===");
		List<SceneVmTemplate> vmUrlList=new ArrayList<>();
		model.addAttribute("vmUrlList", vmUrlList);
		addMessage(redirectAttributes, "生成实例成功");
		return "modules/scene/sceneEnvironmentInstance";

	}
	@ResponseBody
	@RequestMapping(value = "isExperimentExist")
	public JsonModel isExperimentExist(String coursewareId){
		logger.debug("dingh===isExperimentExist=courseid=" + coursewareId);
		JsonModel jsonModel = new JsonModel();
		ResourceCourseware resourceCourseware=resourceCoursewareService.get(coursewareId);
		User user = UserUtils.getUser();
		logger.debug("dingh===isExperimentExist=user=" + user.getId());
		List<SceneEnvironmentInstance> instance=sceneEnvironmentInstanceService.getInstanceByUser(user);

		if(instance!=null&&instance.size()>0)
		{
			//用户已进入一个实验
			if(instance.get(0).getEnvironmentId().equals(resourceCourseware.getEnvironmentId()))
			{
				logger.debug("dingh===isExperimentExist=继续实验=");

				jsonModel.setMsg("1");//继续实验
				jsonModel.setDataObj(instance.get(0).getId());

			} else {
				logger.debug("dingh===isExperimentExist=提示是否新实验=");

				jsonModel.setMsg("2");//提示是否下发新实验
				jsonModel.setDataObj(instance.get(0).getId());

			}
		}else {
			logger.debug("dingh===isExperimentExist=进入实验=");
			jsonModel.setMsg("3");//未进入过实验，进入实验
			jsonModel.setDataObj("");


		}

		return jsonModel;

	}

	//试卷预览-进入实验
	@ResponseBody
	@RequestMapping(value = "isExperimentExistPaper")
	public JsonModel isExperimentExistPaper(String resourceQuestionId){
		logger.debug("dingh===VmId===dd" + resourceQuestionId);
		JsonModel jsonModel = new JsonModel();
		ResourceQuestion resourceQuestion = resourceQuestionService.get(resourceQuestionId);
		User user = UserUtils.getUser();
		logger.debug("dingh===1234ss51=" + user.getId());
		List<SceneEnvironmentInstance> instance=sceneEnvironmentInstanceService.getInstanceByUser(user);

		if(instance!=null&&instance.size()>0)
		{
			//用户已进入一个实验
			if(instance.get(0).getEnvironmentId().equals(resourceQuestion.getEnvironmentId()))
			{
				logger.debug("dingh===1");

				jsonModel.setMsg("1");//继续实验
				jsonModel.setDataObj(instance.get(0).getId());

			} else {
				logger.debug("dingh===2");

				jsonModel.setMsg("2");//提示是否下发新实验
				jsonModel.setDataObj(instance.get(0).getId());

			}
		}else {
			logger.debug("dingh===3");
			jsonModel.setMsg("3");//未进入过实验，进入实验
			jsonModel.setDataObj("");


		}

		return jsonModel;

	}

	@RequestMapping(value = "toSceneExperimentOperationWait")
	public String toSceneExperimentOperationWait (){
		logger.debug("dingh===等待进入实验界面===");

		return "modules/scene/sceneExperimentOperationWait";
	}
	@RequestMapping(value = "toExperimentOperateView")
	public String toExperimentOperateView (Model model,String instanceId,String msg,String coursewareId)  throws RuntimeException,Exception{
		logger.debug("dingh===进入实验界面===" + instanceId + "　" + msg + "　" + coursewareId + "　");
		String document="";
		String coursewareName="";
		ResourceCourseware resourceCourseware=null;
		SceneEnvironment sceneEnvironment=null;
		SceneEnvironmentInstance sceneEnvironmentInstance=null;
		if(msg.equals("3"))
		{
			logger.debug("dingh===进入实验===");

			resourceCourseware=resourceCoursewareService.get(coursewareId);
			sceneEnvironment=sceneEnvironmentService.get(resourceCourseware.getEnvironmentId());
			sceneEnvironmentInstance=new SceneEnvironmentInstance();
			sceneEnvironmentInstance.setCoursewareId(coursewareId);
			sceneEnvironmentInstance.setAutoDelete(SceneConstants.AUTO_DELETE_NO);
			List<SceneVmTemplate> vmUrlList=new ArrayList<>();
			sceneEnvironmentService.createInstance(sceneEnvironment, sceneEnvironmentInstance, vmUrlList);
		}else if(msg.equals("5"))
		{
			logger.debug("dingh===新实验===");

			SceneEnvironmentInstance temp=sceneEnvironmentInstanceService.get(instanceId);
			if(temp!=null&&!temp.equals(""))
				sceneEnvironmentInstanceService.deleteInstance(temp);
			resourceCourseware=resourceCoursewareService.get(coursewareId);
			sceneEnvironment=sceneEnvironmentService.get(resourceCourseware.getEnvironmentId());
			sceneEnvironmentInstance=new SceneEnvironmentInstance();
			sceneEnvironmentInstance.setCoursewareId(coursewareId);
			sceneEnvironmentInstance.setAutoDelete(SceneConstants.AUTO_DELETE_NO);
			List<SceneVmTemplate> vmUrlList=new ArrayList<>();
			sceneEnvironmentService.createInstance(sceneEnvironment, sceneEnvironmentInstance, vmUrlList);

		}else {
			logger.debug("dingh===继续实验===");

			sceneEnvironmentInstance=sceneEnvironmentInstanceService.get(instanceId);
			resourceCourseware=resourceCoursewareService.get(sceneEnvironmentInstance.getCoursewareId());
			sceneEnvironment=sceneEnvironmentService.get(sceneEnvironmentInstance.getEnvironmentId());
		}



		if(resourceCourseware!=null&&!resourceCourseware.equals("")) {
			document = resourceCourseware.getDocument();
			coursewareName=resourceCourseware.getCoursewareName();
		}
		List<SceneEnvironmentInstanceOs> vmList=sceneEnvironmentInstanceService.findOperateMachineByInstanceId(sceneEnvironmentInstance);
		logger.debug("dingh===虚拟机数量===" + vmList.size()+" == "+sceneEnvironmentInstance.getId());

		for(int i=0;i<vmList.size();i++)
		{

			String url=getVmUrl(vmList.get(i).getVmTemplateId());
			if(!url.equals(""))
			{
				logger.debug("dingh===url==111=" +url);
				vmList.get(i).setConsoleUrl(url);
			}
		}

		if(vmList.size()>0)
		{
			sceneEnvironmentInstance.setInstanceOsList(vmList);
		}
		Date orderTime=sceneEnvironmentInstance.getEndDate();
		Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
		ca.setTime(orderTime); //设置时间为当前时间
		int year = ca.get(Calendar.YEAR);//获取年份
		int month=ca.get(Calendar.MONTH)+1;//获取月份
		int day=ca.get(Calendar.DATE);//获取日
		int hour=ca.get(Calendar.HOUR_OF_DAY);//小时
		int minute=ca.get(Calendar.MINUTE);//分
		int second=ca.get(Calendar.SECOND);//秒    2017/05/16 20:27:30
		//"30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务
		String endTime=year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second;




//		List<SceneEnvironmentInstanceOs> vmList=sceneEnvironmentInstanceService.findVmTemplateListByInstanceId(sceneEnvironmentInstance);
		if(sceneEnvironment.getIncreaseTimeAllow().equals("0"))
		{
			model.addAttribute("increaseTime", String.valueOf(Double.parseDouble(sceneEnvironment.getIncreaseTime())*60));
			model.addAttribute("increaseNumber", sceneEnvironment.getIncreaseNumber());
		}

		model.addAttribute("timeDifference", orderTime.getTime() - (new Date()).getTime());

//		System.out.println("----------------" + (orderTime.getTime() - (new Date()).getTime()));


		logger.debug("dingh===结束时间=" + endTime);
		model.addAttribute("coursewareName", coursewareName);
		model.addAttribute("endTime", endTime);
		model.addAttribute("resourceCourseware",resourceCourseware);
		model.addAttribute("sceneEnvironment", sceneEnvironment);
		model.addAttribute("sceneEnvironmentInstance", sceneEnvironmentInstance);
		model.addAttribute("vmList", vmList);
		return "modules/scene/sceneExperimentOperation";
	}

	public String getVmUrl(String vmId){
		logger.debug("dingh===vmId=" + vmId);
		String url="";
		VirtualMachineImpl virtualMachineImpl=new VirtualMachineImpl();
		Server server=virtualMachineImpl.getServerById(vmId,null);
		if(server.getStatus().equals("ACTIVE")||server.getStatus().equals("active"))
		{

			//获取控制台
			ConsolePartImpl showconsoledemo = new ConsolePartImpl();
			System.out.println("获取控制台");

			ConsoleType consoletype = new ConsoleType();


			consoletype.setType("novnc");

			ConsoleIndex getconsole = showconsoledemo.GetConsole(server.getId(), consoletype);
			logger.debug("dingh===获取控制台" + getconsole.getUrl() + " " + getconsole.getType());

			url=getconsole.getUrl();

			SceneEnvironmentInstanceOs instance=sceneEnvironmentInstanceService.getInstanceVm(vmId);
			if(!instance.getConsoleUrl().equals(url))
			{
				logger.debug("dingh===修改url");
				logger.debug("dingh===instance.getConsoleUrl()="+instance.getConsoleUrl());
				logger.debug("dingh===url="+url);


				sceneEnvironmentInstanceService.instanceVmUpdate(getconsole.getUrl(),vmId);
			}
			//修改表实例url
		}else
		{
			logger.debug("dingh===获取控制台失败，重启虚拟机");
			ServerActionsImpl ServerActionsdemo = new ServerActionsImpl();
			ServerActionsdemo.RebootServer(vmId);
			boolean getUrl=true;
			while (getUrl)
			{
				logger.debug("dingh===while");
				server=virtualMachineImpl.getServerById(vmId,null);
				logger.debug("dingh===while1="+server.getStatus());

				if(server.getStatus().equals("ACTIVE")||server.getStatus().equals("active"))
				{

					//获取控制台
					ConsolePartImpl showconsoledemo = new ConsolePartImpl();
					System.out.println("获取控制台");

					ConsoleType consoletype = new ConsoleType();


					consoletype.setType("novnc");

					ConsoleIndex getconsole = showconsoledemo.GetConsole(server.getId(), consoletype);
					logger.debug("dingh===获取控制台" + getconsole.getUrl() + " " + getconsole.getType());

					url=getconsole.getUrl();
					getUrl=false;

					SceneEnvironmentInstanceOs instance=sceneEnvironmentInstanceService.getInstanceVm(vmId);
					if(!instance.getConsoleUrl().equals(url))
					{
						logger.debug("dingh===修改url");
						sceneEnvironmentInstanceService.instanceVmUpdate(getconsole.getUrl(),vmId);
					}

				}else if(server.getStatus().equals("ERROR")||server.getStatus().equals("ERROR"))
				{
					logger.debug("dingh===获取控制台失败");
					getUrl=false;
				}
				else {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}

		return url;
	}
	/**
	 * 增加实验时间
	 */
	@ResponseBody
	@RequestMapping(value = "addTime")
	public String addTime(@RequestParam(required = false) String sceneEnvironmentId,@RequestParam(required = false) String sceneEnvironmentInstanceId) {
		logger.debug("dingh===addTime==" + sceneEnvironmentId + " " + sceneEnvironmentInstanceId);
		SceneEnvironmentInstance sceneEnvironmentInstance=sceneEnvironmentInstanceService.get(sceneEnvironmentInstanceId);
		SceneEnvironment sceneEnvironment=sceneEnvironmentService.get(sceneEnvironmentId);

		Date data=sceneEnvironmentInstance.getEndDate();
		int envTime=(int)(Double.parseDouble(sceneEnvironment.getIncreaseTime())*60);
		Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
		ca.setTime(data); //设置时间为当前时间
		ca.set(Calendar.MINUTE, ca.get(Calendar.MINUTE) + envTime);
		Date endDate=ca.getTime();
		sceneEnvironmentInstance.setEndDate(endDate);

		ScheduleJob scheduleJob= scheduleJobService.findByJobName("env_instance", sceneEnvironmentInstanceId);
		scheduleJob.setStartDate(endDate);
		scheduleJob.setCronExpression(getCancelTime(data, envTime));

		scheduleJobService.save(scheduleJob);
		sceneEnvironmentInstanceService.save(sceneEnvironmentInstance);

		logger.debug("dingh===endDate=" + endDate);
		logger.debug("dingh===getCancelTime=" + getCancelTime(data, envTime));


		ca.setTime(endDate); //设置时间为当前时间
		int year = ca.get(Calendar.YEAR);//获取年份
		int month=ca.get(Calendar.MONTH)+1;//获取月份
		int day=ca.get(Calendar.DATE);//获取日
		int hour=ca.get(Calendar.HOUR_OF_DAY);//小时
		int minute=ca.get(Calendar.MINUTE);//分
		int second=ca.get(Calendar.SECOND);//秒    2017/05/16 20:27:30
		//"30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务
		String endTime=year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second;
		logger.debug("dingh===endTime=" + (endDate.getTime() - (new Date()).getTime()) + "");
		return (endDate.getTime() - (new Date()).getTime()) + "";

	}
	@RequestMapping(value = "toSceneExperimentOperation")
	public String toSceneExperimentOperation (Model model,String instanceId,String msg,String coursewareId)  throws RuntimeException,Exception{


		logger.debug("dingh===结束时间=" + "2017/07/07 12:00:00");
		model.addAttribute("endTime","2017/07/13 12:00:00");
		return "modules/scene/sceneExperimentOperation";
	}
	@RequestMapping(value = "toExperimentOperateViewPaper")
	public String toExperimentOperateViewPaper (Model model,String instanceId,String msg,String resourceQuestionId)  throws RuntimeException,Exception{
		logger.debug("dingh===instanceId1===" + instanceId + "　" + msg + "　" + resourceQuestionId + "　");
		String document="";
		String coursewareName="";
		ResourceQuestion resourceQuestion;
		SceneEnvironment sceneEnvironment=null;
		SceneEnvironmentInstance sceneEnvironmentInstance=null;
		ResourceCourseware resourceCourseware=null;

		if(msg.equals("3"))
		{
			logger.debug("dingh===进入实验===");

			resourceQuestion = resourceQuestionService.get(resourceQuestionId);
			sceneEnvironment=sceneEnvironmentService.get(resourceQuestion.getEnvironmentId());
			sceneEnvironmentInstance=new SceneEnvironmentInstance();
			sceneEnvironmentInstance.setCoursewareId(resourceQuestionId);
			sceneEnvironmentInstance.setAutoDelete(SceneConstants.AUTO_DELETE_NO);
			List<SceneVmTemplate> vmUrlList=new ArrayList<>();
			sceneEnvironmentService.createInstance(sceneEnvironment, sceneEnvironmentInstance, vmUrlList);
		}else if(msg.equals("5"))
		{
			logger.debug("dingh===新实验===");

			SceneEnvironmentInstance temp=sceneEnvironmentInstanceService.get(instanceId);
			if(temp!=null&&!temp.equals(""))
				sceneEnvironmentInstanceService.deleteInstance(temp);
			resourceQuestion = resourceQuestionService.get(resourceQuestionId);
			sceneEnvironment=sceneEnvironmentService.get(resourceQuestion.getEnvironmentId());
			sceneEnvironmentInstance=new SceneEnvironmentInstance();
			sceneEnvironmentInstance.setAutoDelete(SceneConstants.AUTO_DELETE_NO);
			List<SceneVmTemplate> vmUrlList=new ArrayList<>();
			sceneEnvironmentService.createInstance(sceneEnvironment, sceneEnvironmentInstance, vmUrlList);

		}else {
			logger.debug("dingh===继续实验===");
			sceneEnvironmentInstance=sceneEnvironmentInstanceService.get(instanceId);
			resourceQuestion = resourceQuestionService.get(sceneEnvironmentInstance.getCoursewareId());
			sceneEnvironment=sceneEnvironmentService.get(sceneEnvironmentInstance.getEnvironmentId());
		}

		List<SceneEnvironmentInstanceOs> vmList=sceneEnvironmentInstanceService.findOperateMachineByInstanceId(sceneEnvironmentInstance);
		logger.debug("dingh===虚拟机数量===" + vmList.size()+" == "+sceneEnvironmentInstance.getId());

		if(vmList.size()>0)
		{
			sceneEnvironmentInstance.setInstanceOsList(vmList);
		}
		Date orderTime=sceneEnvironmentInstance.getEndDate();
		Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
		ca.setTime(orderTime); //设置时间为当前时间
		int year = ca.get(Calendar.YEAR);//获取年份
		int month=ca.get(Calendar.MONTH)+1;//获取月份
		int day=ca.get(Calendar.DATE);//获取日
		int hour=ca.get(Calendar.HOUR_OF_DAY);//小时
		int minute=ca.get(Calendar.MINUTE);//分
		int second=ca.get(Calendar.SECOND);//秒    2017/05/16 20:27:30
		//"30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务
		String endTime=year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second;


		if(resourceQuestion!=null&&!resourceQuestion.equals("")) {
			document = resourceQuestion.getDocument();
			coursewareName=resourceQuestion.getQuestionName();
			resourceCourseware=new ResourceCourseware();
			resourceCourseware.setDocument(resourceQuestion.getDocument());
			model.addAttribute("resourceCourseware",resourceCourseware);

		}

//		List<SceneEnvironmentInstanceOs> vmList=sceneEnvironmentInstanceService.findVmTemplateListByInstanceId(sceneEnvironmentInstance);
		if(sceneEnvironment.getIncreaseTimeAllow().equals("0"))
		{
			model.addAttribute("increaseTime", String.valueOf(Double.parseDouble(sceneEnvironment.getIncreaseTime())*60));
			model.addAttribute("increaseNumber", sceneEnvironment.getIncreaseNumber());
		}
		logger.debug("dingh===结束时间=" + endTime);
		model.addAttribute("coursewareName", coursewareName);
		model.addAttribute("endTime",endTime);
		model.addAttribute("sceneEnvironment", sceneEnvironment);
		model.addAttribute("sceneEnvironmentInstance", sceneEnvironmentInstance);
		model.addAttribute("vmList", vmList);

		return "modules/scene/sceneExperimentOperation";
	}

//	@RequestMapping(value = "experimentOperateViewInfo")
//	public List<String> experimentOperateViewInfo(String instanceId,String msg,String coursewareId) throws RuntimeException,Exception{
//		logger.debug("dingh===instanceId===" + instanceId + "　" + msg + "　" + coursewareId + "　");
//		List<String> infoList=new ArrayList<>();
//
//		String sceneEnvironmentId="";
//		String sceneEnvironmentInstanceId="";
//		String coursewareName="";
//		String endTime="";
//		String document="";
//
//		if(msg.equals('1'))
//		{
//
//
//			SceneEnvironmentInstance sceneEnvironmentInstance=sceneEnvironmentInstanceService.get(instanceId);
//			ResourceCourseware resourceCourseware=resourceCoursewareService.get(sceneEnvironmentInstance.getCoursewareId());
//			SceneEnvironment sceneEnvironment=sceneEnvironmentService.get(sceneEnvironmentInstance.getEnvironmentId());
//			sceneEnvironmentId=sceneEnvironment.getId();
//			sceneEnvironmentInstanceId=sceneEnvironmentInstance.getId();
//
//			if(resourceCourseware!=null&&!resourceCourseware.equals("")) {
//				document = resourceCourseware.getDocument();
//				coursewareName=resourceCourseware.getCoursewareName();
//			}
//			Date orderTime=sceneEnvironmentInstance.getEndDate();
//			Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
//			ca.setTime(orderTime); //设置时间为当前时间
//			int year = ca.get(Calendar.YEAR);//获取年份
//			int month=ca.get(Calendar.MONTH)+1;//获取月份
//			int day=ca.get(Calendar.DATE);//获取日
//			int hour=ca.get(Calendar.HOUR_OF_DAY);//小时
//			int minute=ca.get(Calendar.MINUTE);//分
//			int second=ca.get(Calendar.SECOND);//秒    2017/05/16 20:27:30
//			//"30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务
//			endTime=year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second;
//
//			logger.debug("dingh===结束时间=" + endTime);
//
//
//
//		}else if(msg.equals('3')){
//
//			ResourceCourseware resourceCourseware=resourceCoursewareService.get(coursewareId);
//			SceneEnvironment sceneEnvironment=sceneEnvironmentService.get(resourceCourseware.getEnvironmentId());
//			SceneEnvironmentInstance sceneEnvironmentInstance=new SceneEnvironmentInstance();
//			sceneEnvironmentInstance.setCoursewareId(coursewareId);
//			sceneEnvironmentInstance.setAutoDelete(SceneConstants.AUTO_DELETE_NO);
//			List<SceneVmTemplate> vmUrlList=new ArrayList<>();
//			sceneEnvironmentService.createInstance(sceneEnvironment, sceneEnvironmentInstance, vmUrlList);
//
//
//		}
//
//		infoList.add(coursewareName);
//		infoList.add(endTime);
//		infoList.add(document);
//		infoList.add(sceneEnvironmentId);
//		infoList.add(sceneEnvironmentInstanceId);
//
//		return infoList;
//	}

	@ResponseBody
	@RequestMapping(value = "experimentOperate")
	public JsonModel experimentOperate(String courseId,String coursewareId) throws RuntimeException,Exception{
		logger.debug("dingh===VmId===dd" + coursewareId + " " + courseId);
		JsonModel jsonModel = new JsonModel();
		ResourceCourseware resourceCourseware=resourceCoursewareService.get(coursewareId);
		User user = UserUtils.getUser();
		logger.debug("dingh===1234ss51=" + user.getId());
		List<SceneEnvironmentInstance> instanceList=sceneEnvironmentInstanceService.getInstanceByUser(user);

		if(instanceList!=null&&instanceList.size()>0)
		{
			//用户已进入一个实验
			if(instanceList.get(0).getEnvironmentId().equals(resourceCourseware.getEnvironmentId()))
			{
				logger.debug("dingh===1");

				jsonModel.setMsg("1");//继续实验
				jsonModel.setDataObj(instanceList.get(0).getId());

			} else {
				logger.debug("dingh===2");

				jsonModel.setMsg("2");//提示是否下发新实验
				jsonModel.setDataObj(instanceList.get(0).getId());

			}
		}else {
			logger.debug("dingh===3");

//
//			SceneEnvironment sceneEnvironment=sceneEnvironmentService.get(resourceCourseware.getEnvironmentId());
//			SceneEnvironmentInstance sceneEnvironmentInstance=new SceneEnvironmentInstance();
//			sceneEnvironmentInstance.setCoursewareId(coursewareId);
//			sceneEnvironmentInstance.setAutoDelete(SceneConstants.AUTO_DELETE_NO);
//			List<SceneVmTemplate> vmUrlList=new ArrayList<>();
//			sceneEnvironmentService.createInstance(sceneEnvironment, sceneEnvironmentInstance, vmUrlList);
//			List<SceneEnvironmentInstanceOs> vmList=sceneEnvironmentInstanceService.findVmTemplateListByInstanceId(sceneEnvironmentInstance);
			jsonModel.setMsg("3");//未进入过实验，进入实验
//			jsonModel.setDataObj(sceneEnvironmentInstance.getId());

		}

		return jsonModel;

	}
	//	@RequiresPermissions("scene:sceneEnvironment:view")
	@RequestMapping(value = "toExperimentOperate")
	public String toExperimentOperate(Model model,String instanceId,String msg,String coursewareId) {
		logger.debug("dingh===instanceId==="+instanceId+"　");
		SceneEnvironmentInstance sceneEnvironmentInstance=sceneEnvironmentInstanceService.get(instanceId);

		ResourceCourseware resourceCourseware=resourceCoursewareService.get(sceneEnvironmentInstance.getCoursewareId());
		SceneEnvironment sceneEnvironment=sceneEnvironmentService.get(sceneEnvironmentInstance.getEnvironmentId());
		String document="";
		String coursewareName="";
		if(resourceCourseware!=null&&!resourceCourseware.equals("")) {
			document = resourceCourseware.getDocument();
			coursewareName=resourceCourseware.getCoursewareName();
		}

//		List<SceneEnvironmentInstanceOs> vmList=sceneEnvironmentInstanceService.findVmTemplateListByInstanceId(sceneEnvironmentInstance);
//		logger.debug("dingh===vm数量=" + vmList.size());
		List<SceneEnvironmentInstanceOs> vmList=sceneEnvironmentInstanceService.findOperateMachineByInstanceId(sceneEnvironmentInstance);
		logger.debug("dingh===vm数量=" + vmList.size());

		if(vmList.size()>0)
		{
			sceneEnvironmentInstance.setInstanceOsList(vmList);
		}
		Date orderTime=sceneEnvironmentInstance.getEndDate();
		Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
		ca.setTime(orderTime); //设置时间为当前时间
		int year = ca.get(Calendar.YEAR);//获取年份
		int month=ca.get(Calendar.MONTH)+1;//获取月份
		int day=ca.get(Calendar.DATE);//获取日
		int hour=ca.get(Calendar.HOUR_OF_DAY);//小时
		int minute=ca.get(Calendar.MINUTE);//分
		int second=ca.get(Calendar.SECOND);//秒    2017/05/16 20:27:30
		//"30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务
		String endTime=year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second;



//		List<SceneEnvironmentInstanceOs> vmList=sceneEnvironmentInstanceService.findVmTemplateListByInstanceId(sceneEnvironmentInstance);

		logger.debug("dingh===结束时间=" + endTime);
		model.addAttribute("coursewareName", coursewareName);
		model.addAttribute("endTime",endTime);
		model.addAttribute("resourceCourseware",resourceCourseware);
		model.addAttribute("sceneEnvironment", sceneEnvironment);
		model.addAttribute("sceneEnvironmentInstance", sceneEnvironmentInstance);
		model.addAttribute("vmList", vmList);

		return "modules/scene/sceneExperimentOperation";
	}
	@ResponseBody
	@RequestMapping(value = "newExperimentOperate")
	public String newExperimentOperate(String instanceId,String coursewareId) throws RuntimeException,Exception{
		logger.debug("dingh===VmId===dd" + coursewareId + " " + instanceId);
		SceneEnvironmentInstance temp=sceneEnvironmentInstanceService.get(instanceId);
		ResourceCourseware resourceCourseware=resourceCoursewareService.get(coursewareId);

		SceneEnvironment sceneEnvironment=sceneEnvironmentService.get(resourceCourseware.getEnvironmentId());
		SceneEnvironmentInstance sceneEnvironmentInstance=new SceneEnvironmentInstance();

		List<SceneVmTemplate> vmUrlList=new ArrayList<>();
		sceneEnvironmentInstance.setAutoDelete(SceneConstants.AUTO_DELETE_NO);
		sceneEnvironmentInstance.setCoursewareId(coursewareId);

		if(temp!=null&&!temp.equals(""))
			sceneEnvironmentInstanceService.deleteInstance(temp);
		sceneEnvironmentService.createInstance(sceneEnvironment, sceneEnvironmentInstance, vmUrlList);
		return sceneEnvironmentInstance.getId();


	}
//	@RequiresPermissions("scene:sceneEnvironment:view")
//	@RequestMapping(value = "toExperimentOperate")
//	public String toExperimentOperate(String environmentId,String courseId ,String coursewareId,Model model) {
//
//		ResourceCourseware resourceCourseware=resourceCoursewareService.get(coursewareId);
////		resourceCourseware.setDocument("&lt;u&gt;&lt;strong&gt;事实上事实上&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;");
//		model.addAttribute("resourceCourseware",resourceCourseware);
//		return "modules/scene/sceneExperimentOperation";
//	}


	public String getDate(String str)
	{
		String result="";
		str="["+str+"]";

		logger.debug("dingh===str="+str);
		try
		{
			JSONArray jsonArray = new JSONArray(str);
			for (int i=0; i < jsonArray.length(); i++)    {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String title = jsonObject.getString("title");
				String type = jsonObject.getString("type");
				String data = jsonObject.getString("data");
				if(type.equals("select"))
				{
					String selected = jsonObject.getString("selected");
					result=selected;

				}else if(type.equals("input"))
				{
					result=data;
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "saveTopology")
	public JsonModel saveTopology(SceneEnvironment sceneEnvironment, Model model, HttpServletRequest httpServletRequest,RedirectAttributes redirectAttributes,String topologyXml) {
		JsonModel jsonModel=new JsonModel();
		InputStream is = null;
//		child:mode
//		classname: module
//		class: module
//		attr_prop_VmTemplate: 1=f4de87a7-d1c4-45d9-8c37-dfa9797bc6c3&&vm424,0=a97a265d-4659-4db6-8fa5-e13804a55c4f&&vm11h,0=0bd53497-da21-4774-9652-1ea332f8d371&&vm1h,0=da6224230a784c7ba0b123c2399678cf&&vm2d,0=608426aac94d43eda4a98cde4b772029&&vm2d,0=157d21583f584664b1781b0431891517&&vm2d,0=208b22ab3e97438b83fca2424a0ad02d&&vm2
//		id: 20
//		title:
//		backImgSrc: http://localhost:8080/static/images/topology/baseMode4.png
//		top: 300
//		left: 626
//		zIndex: 20
//		width: 50
//		height: 50
//
//
//		child:mode
//		classname: module
//		class: module
//		attr_prop_VmTemplate: 1=f4de87a7-d1c4-45d9-8c37-dfa9797bc6c3&&vm424,0=a97a265d-4659-4db6-8fa5-e13804a55c4f&&vm11h,0=0bd53497-da21-4774-9652-1ea332f8d371&&vm1h,0=da6224230a784c7ba0b123c2399678cf&&vm2d,0=608426aac94d43eda4a98cde4b772029&&vm2d,0=157d21583f584664b1781b0431891517&&vm2d,0=208b22ab3e97438b83...1f5e3&&network4h,0=267aedde-187a-4174-b738-10aefdfcbc8f&&network3h
//		id: 16
//		title:
//		backImgSrc: http://localhost:8080/static/images/topology/baseMode3.png
//		top: 246
//		left: 272
//		zIndex: 16
//		width: 50
//		height: 50
//
//
//		child:line
//		strokeweight: 2
//		strokecolor: black
//		brokenType: 1
//		stroke: purple
//		path: M 324 272 L 652 352
//		d: M 324 272 L 652 352
//		id: line14
//		style: cursor: pointer; fill: none; stroke: black; stroke-width: 2;
//		marker-end: url(#arrow)
//		xBaseMode: module16
//		xIndex: 5
//		wBaseMode: module20
//		wIndex: 7
//		attr_prop_attri1: 2
//		attr_prop_attri2: 3
//		attr_prop_attri3: 4
//		attr_prop_attri4: 5
//
//
//		child:line
//		strokeweight: 2
//		strokecolor: black
//		brokenType: 1
//		stroke: purple
//		path: M 298 298 L 534 112
//		d: M 298 298 L 534 112
//		id: line12
//		style: cursor: pointer; fill: none; stroke: black; stroke-width: 2;
//		marker-end: url(#arrow)
//		xBaseMode: module16
//		xIndex: 7
//		wBaseMode: module18
//		wIndex: 4
//		attr_prop_attri1: 2
//		attr_prop_attri2: 3
//		attr_prop_attri3: 4
//		attr_prop_attri4: 5
//		attr_prop_attri1="{&quot;title&quot;:&quot;标题1&quot;,&quot;type&quot;:&quot;select&quot;,&quot;data&quot;:[{&quot;desc&quot;:&quot;教师&quot;,&quot;value&quot;:&quot;0&quot;},{&quot;desc&quot;:&quot;学生&quot;,&quot;value&quot;:&quot;1&quot;}],&quot;selected&quot;:&quot;1&quot;}"
//		attr_prop_attri4="{&quot;title&quot;:&quot;标题4&quot;,&quot;type&quot;:&quot;input&quot;,&quot;data&quot;:&quot;4&quot;}"
//		topologyXml="<?xml version=\"1.0\" encoding=\"utf8\"?><modes><mode classname=\"module\" class=\"module\" attr_prop_VmTemplate=\"1=f4de87a7-d1c4-45d9-8c37-dfa9797bc6c3&amp;&amp;vm424,0=a97a265d-4659-4db6-8fa5-e13804a55c4f&amp;&amp;vm11h,0=0bd53497-da21-4774-9652-1ea332f8d371&amp;&amp;vm1h,0=da6224230a784c7ba0b123c2399678cf&amp;&amp;vm2d,0=608426aac94d43eda4a98cde4b772029&amp;&amp;vm2d,0=157d21583f584664b1781b0431891517&amp;&amp;vm2d,0=208b22ab3e97438b83fca2424a0ad02d&amp;&amp;vm2\" id=\"14\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/baseMode4.png\" top=\"98\" left=\"544\" zIndex=\"14\" width=\"50\" height=\"50\"/><mode classname=\"module\" class=\"module\" attr_prop_Network=\"1=ea1c4a56-a9ae-4f1e-8845-8616aa333699&amp;&amp;network424,0=affb507b-9163-4fae-820b-cba02c8695e1&amp;&amp;net11h,0=2dddfc9a-5558-4427-85dc-d8aef152f4e2&amp;&amp;ss,0=52f973c2-289b-4060-b202-bca53f298290&amp;&amp;network8h,0=1a6dd66d-a61d-4b2f-8e3e-08da2d2651f1&amp;&amp;network6h,0=0d439cc6-a093-407c-9a62-a287e06f0e01&amp;&amp;network5h,0=0c4dd225-b1b5-41b4-baa0-7c7befb1f5e3&amp;&amp;network4h,0=267aedde-187a-4174-b738-10aefdfcbc8f&amp;&amp;network3h\" id=\"12\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/baseMode3.png\" top=\"174\" left=\"283\" zIndex=\"12\" width=\"50\" height=\"50\"/><line strokeweight=\"2\" strokecolor=\"black\" brokenType=\"1\" stroke=\"purple\" path=\"M 283 174 L 596 124\" d=\"M 283 174 L 596 124\" id=\"line12\" style=\"cursor: pointer; fill: none; stroke: black; stroke-width: 2;\" marker-end=\"url(#arrow)\" xBaseMode=\"module12\" xIndex=\"0\" wBaseMode=\"module14\" wIndex=\"5\" attr_prop_attri1=\"2b\" attr_prop_attri2=\"3b\" attr_prop_attri3=\"4s\" attr_prop_attri4=\"5s\"/></modes>";
//		<?xml version='1.0' encoding='utf8'?><modes><mode classname="module" class="module" attr_prop_attri1="{&quot;title&quot;:&quot;标题1&quot;,&quot;type&quot;:&quot;select&quot;,&quot;data&quot;:[{&quot;desc&quot;:&quot;教师&quot;,&quot;value&quot;:&quot;0&quot;},{&quot;desc&quot;:&quot;学生&quot;,&quot;value&quot;:&quot;1&quot;}],&quot;selected&quot;:&quot;1&quot;}" attr_prop_attri2="{&quot;title&quot;:&quot;标题2&quot;,&quot;type&quot;:&quot;select&quot;,&quot;data&quot;:[{&quot;desc&quot;:&quot;教师&quot;,&quot;value&quot;:&quot;0&quot;},{&quot;desc&quot;:&quot;学生&quot;,&quot;value&quot;:&quot;1&quot;}],&quot;selected&quot;:&quot;0&quot;}" attr_prop_attri3="{&quot;title&quot;:&quot;标题3&quot;,&quot;type&quot;:&quot;input&quot;,&quot;data&quot;:&quot;3&quot;}" attr_prop_attri4="{&quot;title&quot;:&quot;标题4&quot;,&quot;type&quot;:&quot;input&quot;,&quot;data&quot;:&quot;4&quot;}" id="12" title="" backImgSrc="file:///F:/%E7%A7%AF%E6%9C%A8%E7%9B%92%E5%AD%90/%E4%BF%A1%E6%81%AF%E5%AE%89%E5%85%A8%E6%95%99%E5%AD%A6%E5%AE%9E%E9%AA%8C%E5%B9%B3%E5%8F%B0/%E5%89%8D%E7%AB%AF/strawberry%20-%20%E5%89%AF%E6%9C%AC/images/baseMode2.png" top="68" left="255" zIndex="12" width="50" height="50"/></modes>
		logger.debug("dingh===topologyXml=:" +topologyXml);
		topologyXml=StringEscapeUtils.unescapeXml(topologyXml);

		logger.debug("反转义HTMLsss:" +topologyXml);
		logger.debug("sceneEnvironmentId:" +sceneEnvironment.getId());

		logger.debug("反转义HTMLsss111:" + StringEscapeUtils.unescapeXml(topologyXml));
		SceneEnvironment temp=sceneEnvironmentService.get(sceneEnvironment);
		if(temp!=null&&temp.getId()!=null)
		{
			//修改
			logger.debug("dingh===修改实验环境");
			logger.debug("dingh==="+temp.getTopologyXml());
			logger.debug("dingh==="+topologyXml);

			if(temp.getTopologyXml().equals(topologyXml))
			{
				sceneEnvironmentService.save(sceneEnvironment);
				jsonModel.setSuccess(true);
				jsonModel.setDataObj(sceneEnvironment.getId());
				return jsonModel;
			}else {
				logger.debug("dingh===delete");
				sceneEnvironmentService.evmRelateDelete(temp);
			}
		}

		List<Mode> modeList=new ArrayList<>();
		List<Line> lineList=new ArrayList<>();

		try {
			is = new ByteArrayInputStream(topologyXml.getBytes("gbk"));
			Reader read = new InputStreamReader(is,"gbk");
			SAXReader reader = new SAXReader();
			Document document = reader.read(read);
			Element root = document.getRootElement();
			List<Element> childElements = root.elements();
			for (Element child : childElements) {
				logger.debug("child:" + child.getName());
				if(child.getName().equals("mode"))
				{
					Mode mode=new Mode();
					mode.setId(child.attributeValue("id"));
					mode.setChild(child.attributeValue("child"));
					mode.setBackImgSrc(child.attributeValue("backImgSrc"));
					mode.setTitle(child.attributeValue("title"));
					mode.setTop(child.attributeValue("top"));
					mode.setLeft(child.attributeValue("left"));
					mode.setWidth(child.attributeValue("width"));
					mode.setHeight(child.attributeValue("height"));
					if(child.attributeValue("backImgSrc").contains("baseMode1.png"))
					{
						String attri=child.attributeValue("attr_prop_attri1");
						String str=getDate(attri);
						if(str==null||str.equals(""))
						{
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("请选择外部网络");
							return jsonModel;
						}
						mode.setType(1);


					}else if(child.attributeValue("backImgSrc").contains("baseMode2.png"))
					{
						String routerName=child.attributeValue("attr_prop_attri1");
						String routerIp=child.attributeValue("attr_prop_attri2");
						String str1=getDate(routerName);
						String str2=getDate(routerIp);

						if(str1==null||str1.equals(""))
						{
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("路由名称不能为空");
							return jsonModel;
						}else {
							if (CheckUtils.isContainChinese(str1)) {
								jsonModel.setSuccess(false);
								jsonModel.setDataObj("路由名称不能包含中文");
								return jsonModel;

							}
						}

						if(str2==null||str2.equals("")) {
//							jsonModel.setSuccess(false);
//							jsonModel.setDataObj("路由IP不能为空");
//							return jsonModel;
						}else {
							mode.setVmIp(str2);

							if(!CheckUtils.isIp(str2))
							{
								jsonModel.setSuccess(false);
								jsonModel.setDataObj("路由IP不合法");
								return jsonModel;

							}
						}
						mode.setType(2);

					}else if(child.attributeValue("backImgSrc").contains("baseMode3.png")||child.attributeValue("backImgSrc").contains("baseMode6.png"))
					{


						String networtName=child.attributeValue("attr_prop_attri1");
						String subnetName=child.attributeValue("attr_prop_attri1");
						String cidr=child.attributeValue("attr_prop_attri2");
						String ipVersion=child.attributeValue("attr_prop_attri3");

						String str1=getDate(networtName);
						String str2=getDate(subnetName);
						String str3=getDate(cidr);
						String str4=getDate(ipVersion);

						if(str1==null||str1.equals(""))
						{
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("交换机名称不能为空");
							return jsonModel;
						}else {
							if (CheckUtils.isContainChinese(str1)) {
								jsonModel.setSuccess(false);
								jsonModel.setDataObj("交换机名称不能包含中文");
								return jsonModel;

							}
						}
						if(str2==null||str2.equals(""))
						{
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("交换机名称不能为空");
							return jsonModel;
						}
						if(str3==null||str3.equals(""))
						{
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("网络地址不能为空");
							return jsonModel;
						}else {
							mode.setCidr(str3);

						}
						if(str4==null||str4.equals(""))
						{
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("IP版本不能为空");
							return jsonModel;
						}
						mode.setType(3);

					}else if(child.attributeValue("backImgSrc").contains("baseMode4.png"))
					{
						//虚拟机
						String vmName=child.attributeValue("attr_prop_attri1");
						String vmTemplate=child.attributeValue("attr_prop_attri2");
						String isOperateMachine=child.attributeValue("attr_prop_attri3");
						String vmIp=child.attributeValue("attr_prop_attri4");

						String str1=getDate(vmName);
						String str2=getDate(vmTemplate);
						String str3=getDate(isOperateMachine);
						String str4=getDate(vmIp);

						if(str1==null||str1.equals("")) {
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("虚拟机名称不能为空");
							return jsonModel;
						}
						if(str4==null||str4.equals("")) {
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("固定IP不能为空");
							return jsonModel;
						}else {
							mode.setVmIp(str4);

							if(!CheckUtils.isIp(str4))
							{
								jsonModel.setSuccess(false);
								jsonModel.setDataObj("虚拟机IP不合法");
								return jsonModel;

							}
						}
						if(str2==null||str2.equals(""))
						{
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("请选择虚拟机模板");
							return jsonModel;
						}
						mode.setType(4);

					}else if(child.attributeValue("backImgSrc").contains("baseMode5.png"))
					{
						//防火墙
						String firewallName=child.attributeValue("attr_prop_attri1");
						String firewallPolicyName=child.attributeValue("attr_prop_attri1");

						String str1=getDate(firewallName);
						String str2=getDate(firewallPolicyName);


						if(str1==null||str1.equals("")) {
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("防火墙名称不能为空");
							return jsonModel;
						}else {
							if (CheckUtils.isContainChinese(str1)) {
								jsonModel.setSuccess(false);
								jsonModel.setDataObj("防火墙名称不能包含中文");
								return jsonModel;

							}
						}
						if(str2==null||str2.equals("")) {
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("防火墙名称不能为空");
							return jsonModel;
						}
						mode.setType(5);


					}
					modeList.add(mode);


				}else if(child.getName().equals("line")){

					Line line=new Line();
					line.setId(child.attributeValue("id"));
					line.setChild(child.attributeValue("child"));
					line.setPath(child.attributeValue("path"));
					lineList.add(line);
				}

			}
			for(int i=0;i<lineList.size();i++)
			{
				Line line=lineList.get(i);

				String path=line.getPath();//path: M 298 298 L 534 112
				String[] local=path.split(" ");
				int left1=Integer.parseInt(local[1]);
				int top1=Integer.parseInt(local[2]);

				int left2=Integer.parseInt(local[4]);
				int top2=Integer.parseInt(local[5]);
				logger.debug("line:"+left1+" "+top1+" "+left2+" "+top2+" ");

				String modeId1="",modeId2="";
				int index1=0,index2=0;
				Mode relateMode1=null,relateMode2=null;
				boolean findModeA=false;
				boolean findModeB=false;

				for(int j=0;j<modeList.size();j++)
				{

					Mode mode=modeList.get(j);
					int modeLeft=Integer.parseInt(mode.getLeft());
					int modeTop=Integer.parseInt(mode.getTop());
					logger.debug("mode:"+modeLeft+" "+modeTop);

					if((modeLeft-5)<=left1&&left1<+(modeLeft+50+5)&&(modeTop-5)<=top1&&top1<+(modeTop+50+5))
					{
						index1=j;
						modeId1=modeList.get(j).getId();
						relateMode1=modeList.get(j);
						logger.debug("找到modeA="+modeList.get(j).getId());
						findModeA=true;
					}
					if((modeLeft-5)<=left2&&left2<+(modeLeft+50+5)&&(modeTop-5)<=top2&&top2<+(modeTop+50+5))
					{
						index2=j;
						modeId2=modeList.get(j).getId();
						relateMode2=modeList.get(j);
						logger.debug("找到modeB="+modeList.get(j).getId());
						findModeB=true;

					}
				}
				if(findModeA==false||findModeB==false)
				{
					jsonModel.setSuccess(false);
					jsonModel.setDataObj("连线两端未都连接，请检查");
					return jsonModel;
				}
				modeList.get(index1).setRelateModeId(modeId2);
				modeList.get(index2).setRelateModeId(modeId1);
				int type1=modeList.get(index1).getType();
				int type2=modeList.get(index2).getType();
				switch (type1)
				{
					case 1:
						modeList.get(index1).setRelateModeRight(relateMode2);
						modeList.get(index2).setRelateModeLeft(relateMode1);

						break;
					case 2:
						switch (type2)
						{
							case 1:
								modeList.get(index1).setRelateModeLeft(relateMode2);
								modeList.get(index2).setRelateModeRight(relateMode1);
								break;
							case 2:
								break;
							case 3:
								modeList.get(index1).setRelateModeRight(relateMode2);
								modeList.get(index2).setRelateModeLeft(relateMode1);
								break;
							case 4:
								break;
							case 5:
								modeList.get(index1).setRelateModeLeft(relateMode2);
								modeList.get(index2).setRelateModeRight(relateMode1);
								break;
						}
						break;
					case 3:
						switch (type2)
						{
							case 1:
								break;
							case 2:
								modeList.get(index1).setRelateModeLeft(relateMode2);
								modeList.get(index2).setRelateModeRight(relateMode1);
								break;
							case 3:
								break;
							case 4:
								modeList.get(index1).setRelateModeRight(relateMode2);
								modeList.get(index2).setRelateModeLeft(relateMode1);
								break;
							case 5:
								modeList.get(index1).setRelateModeRight(relateMode2);
								modeList.get(index2).setRelateModeLeft(relateMode1);
								break;
						}
						break;
					case 4:
						modeList.get(index1).setRelateModeLeft(relateMode2);
						modeList.get(index2).setRelateModeRight(relateMode1);
						break;
					case 5:
						switch (type2)
						{
							case 1:
								modeList.get(index1).setRelateModeLeft(relateMode2);
								modeList.get(index2).setRelateModeRight(relateMode1);
								break;
							case 2:
								modeList.get(index1).setRelateModeRight(relateMode2);
								modeList.get(index2).setRelateModeLeft(relateMode1);
								break;
							case 3:
								break;
							case 4:
								break;
							case 5:
								break;
						}
						break;
				}

			}
			for(int i=0; i<modeList.size();i++)
			{
				if(modeList.get(i).getType()==4)
				{
					if(modeList.get(i).getRelateModeLeft()==null)
					{
						jsonModel.setSuccess(false);
						jsonModel.setDataObj("虚拟机必须连接交换机");
						return jsonModel;
					}else{
						try{
							logger.debug("dingh===d="+modeList.get(i).getVmIp()+"="+modeList.get(i).getRelateModeLeft().getCidr());
							if(!CheckUtils.isIpInRange(modeList.get(i).getVmIp(),modeList.get(i).getRelateModeLeft().getCidr()))
							{
								jsonModel.setSuccess(false);
								jsonModel.setDataObj("虚拟机IP不在交换机网络地址内");
								return jsonModel;
							}
						}catch (Exception e)
						{
							jsonModel.setSuccess(false);
							jsonModel.setDataObj("虚拟机IP不在交换机网络地址内，请检查网段是否正确");
							return jsonModel;
						}
					}
				}else
				{
					continue;
				}
			}

			//111111=========================================
			logger.debug("dingh===123123111111111");
			modeList=new ArrayList<>();
			lineList=new ArrayList<>();
			sceneEnvironment.setTopologyXml(topologyXml);
			sceneEnvironmentService.save(sceneEnvironment);
			logger.debug("dingh===sceneEnvironmentId" + sceneEnvironment.getId());
			for (Element child : childElements) {
				logger.debug("child:" + child.getName());



				if(child.getName().equals("mode"))
				{
					Mode mode=new Mode();
					mode.setId(child.attributeValue("id"));
					mode.setChild(child.attributeValue("child"));
					mode.setBackImgSrc(child.attributeValue("backImgSrc"));
					mode.setTitle(child.attributeValue("title"));
					mode.setTop(child.attributeValue("top"));
					mode.setLeft(child.attributeValue("left"));
					mode.setWidth(child.attributeValue("width"));
					mode.setHeight(child.attributeValue("height"));

					if(child.attributeValue("backImgSrc").contains("baseMode1.png"))
					{
						mode.setType(1);
						String attri=child.attributeValue("attr_prop_attri1");
						String str=getDate(attri);
						if(str!=null&&!str.equals(""))
						{

							//外部网络
							mode.setPublicNetwork(str);
							logger.debug("dingh===setAttr_prop_外部网络:" + str);
						}


					}else if(child.attributeValue("backImgSrc").contains("baseMode2.png"))
					{
						mode.setType(2);

						String routerName=child.attributeValue("attr_prop_attri1");
						String routerIp=child.attributeValue("attr_prop_attri2");
						String str1=getDate(routerName);
						String str2=getDate(routerIp);

						if(str1!=null&&!str1.equals(""))
						{
							mode.setRouterName(str1);
							SceneRouter sceneRouter=new SceneRouter();
							sceneRouter.setRouterName(str1);
							sceneRouterService.save(sceneRouter);
							mode.setEnvId(sceneRouter.getId());
						}
						mode.setRouterIp(str2);
						logger.debug("dingh===新建路由id:" + mode.getEnvId());
						logger.debug("dingh===routerName:" + str1);


					}else if(child.attributeValue("backImgSrc").contains("baseMode3.png")||child.attributeValue("backImgSrc").contains("baseMode6.png"))
					{
						mode.setType(3);

						String networtName=child.attributeValue("attr_prop_attri1");
						String subnetName=child.attributeValue("attr_prop_attri1");
						String cidr=child.attributeValue("attr_prop_attri2");
						String ipVersion=child.attributeValue("attr_prop_attri3");

						String str1=getDate(networtName);
						String str2=getDate(subnetName);
						String str3=getDate(cidr);
						String str4=getDate(ipVersion);

						if(str1!=null&&!str1.equals(""))
						{
							mode.setNetwortName(str1);
							logger.debug("dingh===networtName:" + str1);
						}
						if(str2!=null&&!str2.equals(""))
						{
							mode.setSubnetName(str2);
							logger.debug("dingh===subnetName:" + str2);
						}
						if(str3!=null&&!str3.equals(""))
						{
							mode.setCidr(str3);
							logger.debug("dingh===cidr:" +str3);
						}
						if(str4!=null&&!str4.equals(""))
						{
							mode.setIpVersion(str4);
							logger.debug("dingh===ipVersion:" + str4);
						}
						SceneNetwork sceneNetwork=new SceneNetwork();
						sceneNetwork.setNetworkName(mode.getNetwortName());
						sceneNetwork.setType(SceneConstants.networkTypeNormal);
						sceneNetworkService.save(sceneNetwork);

						SceneSubnet sceneSubnet = new SceneSubnet();
						sceneSubnet.setSubnetName(mode.getSubnetName());
						sceneSubnet.setIpVersion(mode.getIpVersion());
						sceneSubnet.setCidr(mode.getCidr());
						sceneSubnet.setNetworkId(sceneNetwork.getId());
						sceneSubnetService.save(sceneSubnet);

						mode.setEnvId(sceneNetwork.getId());
						logger.debug("dingh===网络id:[{}],子网id:[{}]",sceneNetwork.getId(),sceneSubnet.getId());


					}else if(child.attributeValue("backImgSrc").contains("baseMode4.png"))
					{
						mode.setType(4);

						//虚拟机
						String vmName=child.attributeValue("attr_prop_attri1");
						String vmTemplate=child.attributeValue("attr_prop_attri2");
						String isOperateMachine=child.attributeValue("attr_prop_attri3");
						String vmIp=child.attributeValue("attr_prop_attri4");

						String str1=getDate(vmName);
						String str2=getDate(vmTemplate);
						String str3=getDate(isOperateMachine);
						String str4=getDate(vmIp);

						if(str1!=null&&!str1.equals("")) {
							logger.debug("dingh===vmName:" + str1);
							mode.setVmName(str1);
						}
						if(str4!=null&&!str4.equals("")) {
							logger.debug("dingh===vmIp:" + str4);
							mode.setVmIp(str4);
						}
						if(str2!=null&&!str2.equals(""))
						{
							logger.debug("dingh===attr_prop_虚拟机:" + str2);
							mode.setVmTemplate(str2);
							mode.setEnvId(str2);
						}
						if(str3!=null&&!str3.equals(""))
						{
							logger.debug("dingh===attr_prop_虚拟机:" + str3);
							mode.setIsOperateMachine(str3);
						}

					}else if(child.attributeValue("backImgSrc").contains("baseMode5.png"))
					{
						mode.setType(5);

						//防火墙

						String firewallName=child.attributeValue("attr_prop_attri1");
						String firewallPolicyName=child.attributeValue("attr_prop_attri1");

						String str1=getDate(firewallName);
						String str2=getDate(firewallPolicyName);

						if(str1!=null&&!str1.equals("")) {
							logger.debug("dingh===firewallName:" + str1);
							mode.setFirewallName(str1);
						}
						if(str2!=null&&!str2.equals("")) {
							logger.debug("dingh===firewallPolicyName:" + str2);
							mode.setFirewallPolicyName(str2);
						}
						SceneFirewall sceneFirewall=new SceneFirewall();
						String firewallId= IdGen.uuid();
						sceneFirewall.setId(firewallId);
						sceneFirewall.setFirewallName(mode.getFirewallName());
						sceneFirewall.setFirewallPolicyName(mode.getFirewallPolicyName());
						sceneRouterService.firewallInsert(sceneFirewall);
						mode.setEnvId(firewallId);
					}
					modeList.add(mode);

				}else if(child.getName().equals("line")){

					Line line=new Line();
					line.setId(child.attributeValue("id"));
					line.setChild(child.attributeValue("child"));
					line.setPath(child.attributeValue("path"));
					lineList.add(line);
				}

			}
			logger.debug("size:" + modeList.size());
			logger.debug("size:" + lineList.size());

			for(int i=0;i<lineList.size();i++)
			{
				Line line=lineList.get(i);

				String path=line.getPath();//path: M 298 298 L 534 112
				String[] local=path.split(" ");
				int left1=Integer.parseInt(local[1]);
				int top1=Integer.parseInt(local[2]);

				int left2=Integer.parseInt(local[4]);
				int top2=Integer.parseInt(local[5]);
				logger.debug("line:"+left1+" "+top1+" "+left2+" "+top2+" ");

				String modeId1="",modeId2="";
				int index1=0,index2=0;
				Mode relateMode1=null,relateMode2=null;
				boolean findModeA=false;
				boolean findModeB=false;

				for(int j=0;j<modeList.size();j++)
				{

					Mode mode=modeList.get(j);
					int modeLeft=Integer.parseInt(mode.getLeft());
					int modeTop=Integer.parseInt(mode.getTop());
					logger.debug("mode:"+modeLeft+" "+modeTop);

					if((modeLeft-5)<=left1&&left1<+(modeLeft+50+5)&&(modeTop-5)<=top1&&top1<+(modeTop+50+5))
					{
						index1=j;
						modeId1=modeList.get(j).getId();
						relateMode1=modeList.get(j);
						logger.debug("找到modeA="+modeList.get(j).getId());
						findModeA=true;
					}
					if((modeLeft-5)<=left2&&left2<+(modeLeft+50+5)&&(modeTop-5)<=top2&&top2<+(modeTop+50+5))
					{
						index2=j;
						modeId2=modeList.get(j).getId();
						relateMode2=modeList.get(j);
						logger.debug("找到modeB="+modeList.get(j).getId());
						findModeB=true;

					}
				}
				int type1=modeList.get(index1).getType();
				int type2=modeList.get(index2).getType();
				if(modeList.get(index1).getList()==null)
				{
					List<Mode> list=new ArrayList<Mode>();
					list.add(modeList.get(index2));
					modeList.get(index1).setList(list);
				}else{
					modeList.get(index1).getList().add(modeList.get(index2));
				}
				if(modeList.get(index2).getList()==null)
				{
					List<Mode> list=new ArrayList<Mode>();
					list.add(modeList.get(index1));
					modeList.get(index2).setList(list);
				}else{
					modeList.get(index2).getList().add(modeList.get(index1));
				}
				if(type1==2&&type2==2)
				{
					SceneNetwork sceneNetwork=new SceneNetwork();
					sceneNetwork.setNetworkName(modeList.get(index1).getRouterName()+"-"+modeList.get(index2).getRouterName());
					sceneNetwork.setType(SceneConstants.networkTypeVirtual);
					sceneNetworkService.save(sceneNetwork);
					sceneEnvironmentService.evmNetworkInsert(sceneEnvironment.getId(), sceneNetwork.getId());

					modeList.get(index1).setRouterNetworkId(sceneNetwork.getId());
					modeList.get(index2).setRouterNetworkId(sceneNetwork.getId());
				}


//				modeList.get(index1).setRelateModeId(modeId2);
//				modeList.get(index2).setRelateModeId(modeId1);
//
//				switch (type1)
//				{
//					case 1:
//						modeList.get(index1).setRelateModeRight(relateMode2);
//						modeList.get(index2).setRelateModeLeft(relateMode1);
//
//						break;
//					case 2:
//						switch (type2)
//						{
//							case 1:
//								modeList.get(index1).setRelateModeLeft(relateMode2);
//								modeList.get(index2).setRelateModeRight(relateMode1);
//								break;
//							case 2:
//								SceneNetwork sceneNetwork=new SceneNetwork();
//								sceneNetwork.setNetworkName(modeList.get(index1).getRouterName()+"-"+modeList.get(index2).getRouterName());
//								sceneNetwork.setType(SceneConstants.networkTypeVirtual);
//								sceneNetworkService.save(sceneNetwork);
//								sceneEnvironmentService.evmNetworkInsert(sceneEnvironment.getId(), sceneNetwork.getId());
//
//								modeList.get(index1).setRouterNetworkId(sceneNetwork.getId());
//								modeList.get(index2).setRouterNetworkId(sceneNetwork.getId());
//
//								modeList.get(index1).setRelateModeLeft(relateMode2);
//								modeList.get(index2).setRelateModeLeft(relateMode1);
//								break;
//							case 3:
//								modeList.get(index1).setRelateModeRight(relateMode2);
//								modeList.get(index2).setRelateModeLeft(relateMode1);
//								break;
//							case 4:
//								break;
//							case 5:
//								modeList.get(index1).setRelateModeLeft(relateMode2);
//								modeList.get(index2).setRelateModeRight(relateMode1);
//								break;
//						}
//						break;
//					case 3:
//						switch (type2)
//						{
//							case 1:
//								break;
//							case 2:
//								modeList.get(index1).setRelateModeLeft(relateMode2);
//								modeList.get(index2).setRelateModeRight(relateMode1);
//								break;
//							case 3:
//								break;
//							case 4:
//								modeList.get(index1).setRelateModeRight(relateMode2);
//								modeList.get(index2).setRelateModeLeft(relateMode1);
//								break;
//							case 5:
//								modeList.get(index1).setRelateModeRight(relateMode2);
//								modeList.get(index2).setRelateModeLeft(relateMode1);
//								break;
//						}
//						break;
//					case 4:
//						modeList.get(index1).setRelateModeLeft(relateMode2);
//						modeList.get(index2).setRelateModeRight(relateMode1);
//						break;
//					case 5:
//						switch (type2)
//						{
//							case 1:
//								modeList.get(index1).setRelateModeLeft(relateMode2);
//								modeList.get(index2).setRelateModeRight(relateMode1);
//								break;
//							case 2:
//								modeList.get(index1).setRelateModeRight(relateMode2);
//								modeList.get(index2).setRelateModeLeft(relateMode1);
//								break;
//							case 3:
//								break;
//							case 4:
//								break;
//							case 5:
//								break;
//						}
//						break;
//				}

				logger.debug("line-mode:"+modeId1+" "+modeId2);

			}

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
//		for(int i=0; i<modeList.size();i++)
//		{
//			if(modeList.get(i).getType()==5)
//			{
//
//			}
//		}

		for(int i=0; i<modeList.size();i++)
		{
			switch (modeList.get(i).getType())
			{
				case 1:

					break;
				case 2:

					logger.debug("dingh===case2:" + modeList.get(i).getEnvId());
					List<Mode> list=modeList.get(i).getList();
					SceneNetwork sceneNetwork=new SceneNetwork();
					if(list!=null&&list.size()>0)
					{
						for(int j=0;j<list.size();j++)
						{
							Mode mode=list.get(j);
							if(mode.getType()==1)
							{
								logger.debug("dingh===添加网关:" + mode.getPublicNetwork());
								SceneRouter sceneRouter=sceneRouterService.get(modeList.get(i).getEnvId());
								sceneRouter.setNetworkId(mode.getPublicNetwork());
								sceneRouterService.save(sceneRouter);
							}
							if(mode.getType()==2) {
								sceneNetwork.setId(modeList.get(i).getRouterNetworkId());
								String ip=modeList.get(i).getRouterIp();
								String cidr=ip.substring(0, ip.lastIndexOf("."))+".0/24";
								List<SceneSubnet> sceneSubnetList=sceneSubnetService.findSubnetListByNetworkId(sceneNetwork);

								boolean addSubnet=true;
								if(sceneSubnetList.size()>0)
								{
									for(int k=0;k<sceneSubnetList.size();k++)
									{
										if(sceneSubnetList.get(k).getCidr().equals(cidr))
										{
											addSubnet=false;
											break;
										}
									}
								}
								if(addSubnet)
								{
									SceneSubnet sceneSubnet = new SceneSubnet();
									sceneSubnet.setSubnetName("snet-"+modeList.get(i).getRouterName());
									sceneSubnet.setIpVersion("4");

									logger.debug("dingh===ip:" + ip);

									sceneSubnet.setCidr(cidr);
									sceneSubnet.setNetworkId(modeList.get(i).getRouterNetworkId());
									sceneSubnetService.save(sceneSubnet);
								}


								//保存端口
								ScenePort scenePort=new ScenePort();
								scenePort.setNetworkId(modeList.get(i).getRouterNetworkId());
								scenePort.setFixedIp(modeList.get(i).getRouterIp());
								scenePortService.save(scenePort);

								sceneRouterService.routerPortSave(modeList.get(i).getEnvId(),scenePort.getId(),modeList.get(i).getRouterIp());


							}
							if(mode.getType()==3)
							{
								sceneNetwork.setId(mode.getEnvId());
								List<SceneSubnet> sceneSubnetList=sceneSubnetService.findSubnetListByNetworkId(sceneNetwork);

								for(int k=0;k<sceneSubnetList.size();k++)
								{
									logger.debug("dingh===路由增加接口:" );
									SceneSubnet sceneSubnet=sceneSubnetList.get(k);
									List<SceneSubnet> routerSubnetList=sceneSubnetService.findRouterSubnet(modeList.get(i).getEnvId(), sceneSubnet.getId());
									if (routerSubnetList.size()==0)
										sceneRouterService.saveInterface(modeList.get(i).getEnvId(), sceneSubnet.getId());

								}
							}
							if(mode.getType()==5) {
								if(mode.getList()!=null)
								{

									for(int k=0;k<mode.getList().size();k++)
									{
										Mode mode1=mode.getList().get(k);
										if(mode1.getType()==1)
										{
											logger.debug("dingh===添加网关:" + mode1.getPublicNetwork());
											SceneRouter sceneRouter=sceneRouterService.get(modeList.get(i).getEnvId());
											sceneRouter.setNetworkId(mode1.getPublicNetwork());
											sceneRouterService.save(sceneRouter);
										}
										if(mode1.getType()==3)
										{
											sceneNetwork.setId(mode1.getEnvId());
											List<SceneSubnet> sceneSubnetList=sceneSubnetService.findSubnetListByNetworkId(sceneNetwork);
												logger.debug("dingh===路由增加接口:" );
												SceneSubnet sceneSubnet=sceneSubnetList.get(0);
												List<SceneSubnet> routerSubnetList=sceneSubnetService.findRouterSubnet(modeList.get(i).getEnvId(), sceneSubnet.getId());
												if (routerSubnetList.size()==0)
													sceneRouterService.saveInterface(modeList.get(i).getEnvId(), sceneSubnet.getId());
										}
									}
								}
							}
						}
					}


//					SceneNetwork sceneNetwork=new SceneNetwork();
//					if(modeList.get(i).getRelateModeLeft()!=null&&modeList.get(i).getRelateModeLeft().getType()==1)
//					{
//						logger.debug("dingh===添加网关:" + modeList.get(i).getRelateModeLeft().getPublicNetwork());
//						SceneRouter sceneRouter=sceneRouterService.get(modeList.get(i).getEnvId());
//						sceneRouter.setNetworkId(modeList.get(i).getRelateModeLeft().getPublicNetwork());
//						sceneRouterService.save(sceneRouter);
//					}
//					if(modeList.get(i).getRelateModeLeft()!=null&&modeList.get(i).getRelateModeLeft().getType()==2)
//					{
//
//						SceneSubnet sceneSubnet = new SceneSubnet();
//						sceneSubnet.setSubnetName("snet-"+modeList.get(i).getRouterName());
//						sceneSubnet.setIpVersion("4");
//						String ip=modeList.get(i).getRouterIp();
//						logger.debug("dingh===ip:" + ip);
//						String cidr=ip.substring(0, ip.lastIndexOf("."))+".0/24";
//						sceneSubnet.setCidr(cidr);
//						sceneSubnet.setNetworkId(modeList.get(i).getRouterNetworkId());
//						sceneSubnetService.save(sceneSubnet);
//
//						//保存端口
//						ScenePort scenePort=new ScenePort();
//						scenePort.setNetworkId(modeList.get(i).getRouterNetworkId());
//						scenePort.setFixedIp(modeList.get(i).getRouterIp());
//						scenePortService.save(scenePort);
//
//						sceneRouterService.routerPortSave(modeList.get(i).getEnvId(),scenePort.getId(),modeList.get(i).getRouterIp());
//
//
//
//					}
//					if(modeList.get(i).getRelateModeLeft()!=null&&modeList.get(i).getRelateModeLeft().getType()==5)
//					{
//						if(modeList.get(i).getRelateModeLeft().getRelateModeLeft()!=null&&modeList.get(i).getRelateModeLeft().getRelateModeLeft().getType()==1)
//						{
//							logger.debug("dingh===添加网关:" + modeList.get(i).getRelateModeLeft().getRelateModeLeft().getPublicNetwork());
//							SceneRouter sceneRouter=sceneRouterService.get(modeList.get(i).getEnvId());
//							sceneRouter.setNetworkId(modeList.get(i).getRelateModeLeft().getRelateModeLeft().getPublicNetwork());
//							sceneRouterService.save(sceneRouter);
//						}
//					}
//					if(modeList.get(i).getRelateModeRight()!=null&&modeList.get(i).getRelateModeRight().getEnvId()!=null)
//					{
//						sceneNetwork.setId(modeList.get(i).getRelateModeRight().getEnvId());
//						List<SceneSubnet> sceneSubnetList=sceneSubnetService.findSubnetListByNetworkId(sceneNetwork);
//
//						for(int j=0;j<sceneSubnetList.size();j++)
//						{
//							logger.debug("dingh===路由增加接口:" );
//							SceneSubnet sceneSubnet=sceneSubnetList.get(j);
//							List<SceneSubnet> routerSubnetList=sceneSubnetService.findRouterSubnet(modeList.get(i).getEnvId(), sceneSubnet.getId());
//							if (routerSubnetList.size()==0)
//								sceneRouterService.saveInterface(modeList.get(i).getEnvId(), sceneSubnet.getId());
//
//						}
//					}
					sceneEnvironmentService.evmRouterInsert(sceneEnvironment.getId(), modeList.get(i).getEnvId());
					break;
				case 3:
					logger.debug("dingh===case3:" + modeList.get(i).getEnvId());
					sceneEnvironmentService.evmNetworkInsert(sceneEnvironment.getId(), modeList.get(i).getEnvId());

					break;
				case 4:
					logger.debug("dingh===vm虚拟机:");
					logger.debug("dingh===case4:" + modeList.get(i).getEnvId());
					//保存端口

					list=modeList.get(i).getList();
					if(list!=null&&list.size()>0) {
						for (int j = 0; j < list.size(); j++) {
							Mode mode = list.get(j);
							if (mode.getType() == 3) {
								ScenePort scenePort=new ScenePort();
								scenePort.setNetworkId(mode.getEnvId());
								scenePort.setFixedIp(modeList.get(i).getVmIp());
								scenePortService.save(scenePort);
								sceneVmTemplateService.vmNetworkInsert(modeList.get(i).getEnvId(), mode.getEnvId(), sceneEnvironment.getId());
								sceneEnvironmentService.evmVmInsert(sceneEnvironment.getId(), modeList.get(i).getEnvId(), modeList.get(i).getIsOperateMachine(), scenePort.getId(), modeList.get(i).getVmName());

							}
							if(mode.getType() == 5)
							{
								if(mode.getList()!=null)
								{

									for(int k=0;k<mode.getList().size();k++)
									{
										Mode mode1=mode.getList().get(k);
										if (mode1.getType()==3)
										{
											ScenePort scenePort=new ScenePort();
											scenePort.setNetworkId(mode.getEnvId());
											scenePort.setFixedIp(modeList.get(i).getVmIp());
											scenePortService.save(scenePort);
											sceneVmTemplateService.vmNetworkInsert(modeList.get(i).getEnvId(), mode1.getEnvId(), sceneEnvironment.getId());
											sceneEnvironmentService.evmVmInsert(sceneEnvironment.getId(), modeList.get(i).getEnvId(), modeList.get(i).getIsOperateMachine(), scenePort.getId(), modeList.get(i).getVmName());

										}
									}
								}
							}
						}
					}
//					ScenePort scenePort=new ScenePort();
//					scenePort.setNetworkId(modeList.get(i).getRelateModeLeft().getEnvId());
//					scenePort.setFixedIp(modeList.get(i).getVmIp());
//					scenePortService.save(scenePort);
//
//					sceneVmTemplateService.vmNetworkInsert(modeList.get(i).getEnvId(), modeList.get(i).getRelateModeLeft().getEnvId(),sceneEnvironment.getId());

					break;
				case 5:
					logger.debug("dingh===case5:" + modeList.get(i).getEnvId());
					list=modeList.get(i).getList();
					if(list!=null&&list.size()>0) {
						for (int j = 0; j < list.size(); j++) {
							Mode mode = list.get(j);
							if (mode.getType() == 2) {
								sceneRouterService.routerFirewallInsert(modeList.get(i).getEnvId(), mode.getEnvId());
							}
						}
					}

//					sceneEnvironmentService.evmSgInsert(sceneEnvironment.getId(), modeList.get(i).getEnvId());

					break;
			}
		}
		jsonModel.setSuccess(true);
		jsonModel.setDataObj(sceneEnvironment.getId());
		return jsonModel;


	}

	@RequiresPermissions("scene:sceneEnvironment:view")
	@RequestMapping(value = "scenetTest")
	public String scenetTest(Model model ) {
		ResourceCourseware resourceCourseware=new ResourceCourseware();
	    model.addAttribute("resourceCourseware",resourceCourseware);
		return "modules/scene/sceneExperimentOperationWait";
	}
	@RequiresPermissions("scene:sceneEnvironment:view")
	@RequestMapping(value = "detail")
	public String detail(SceneEnvironment sceneEnvironment, Model model) {
		logger.debug("dingh===detail===");

		List<SceneNetwork> sceneNetworkList=sceneNetworkService.findEnvironmentNetworkDetail(DelFlag.DEL_FLAG_NORMAL, sceneEnvironment.getId());
		List<SceneRouter> sceneRouterList=sceneRouterService.findRouterListByEnvironmentId(DelFlag.DEL_FLAG_NORMAL,sceneEnvironment.getId());
		List<SceneVmTemplate> sceneVmTemplateList=sceneVmTemplateService.findVMListByEnvironmentId(DelFlag.DEL_FLAG_NORMAL,sceneEnvironment.getId());

		for(int i=0;i<sceneNetworkList.size();i++)
		{
			SceneSubnet sceneSubnet=new SceneSubnet();
			sceneSubnet.setNetworkId(sceneNetworkList.get(i).getId());
			List<SceneSubnet> sceneSubnet1=sceneSubnetService.findList(sceneSubnet);
			sceneNetworkList.get(i).setSubnetName(sceneSubnet1.get(0).getSubnetName());
			sceneNetworkList.get(i).setCidr(sceneSubnet1.get(0).getCidr());


		}
		for(int i=0;i<sceneRouterList.size();i++)
		{

			SceneSubnet sceneSubnet=new SceneSubnet();
			sceneSubnet.setRouterId(sceneRouterList.get(i).getId());
			List<SceneSubnet> sceneSubnet1=sceneSubnetService.getSubnetListByRouterId(sceneSubnet);
			if(sceneSubnet1.size()>0) {
				sceneRouterList.get(i).setSubnetName(sceneSubnet1.get(0).getSubnetName());
				sceneRouterList.get(i).setCidr(sceneSubnet1.get(0).getCidr());
			}
		}
//		for(int i=0;i<sceneVmTemplateList.size();i++)
//		{
//			SceneVmType type=sceneVmTypeService.get(sceneVmTemplateList.get(i).getFlavorRef());
//			sceneVmTemplateList.get(i).setRam(type.getRam());
//			sceneVmTemplateList.get(i).setVcpus(type.getVcpus());
//			sceneVmTemplateList.get(i).setDisk(type.getDisk());
//			sceneVmTemplateList.get(i).setVmTypeName(type.getName());
////			ScenePort scenePort=scenePortService.get(sceneVmTemplateList.get(i).getPortId());
////			sceneVmTemplateList.get(i).setPortId(scenePort.getFixedIp());
//
//		}
		model.addAttribute("sceneNetworkList", sceneNetworkList);
		model.addAttribute("sceneRouterList", sceneRouterList);
		model.addAttribute("sceneVmTemplateList", sceneVmTemplateList);

		model.addAttribute("sceneEnvironment", sceneEnvironment);
		return "modules/scene/sceneEnvironmentDetail";
	}
	@RequestMapping(value = "toExperimentOperateTest")
	public String toExperimentOperateTest(Model model,String courseId,String coursewareId,String instanceId) {

		model.addAttribute("coursewareName", "安卓入门到精通");

		model.addAttribute("document", "&lt;em&gt;444444&lt;/em&gt;&lt;/p&gt;");
		return "modules/scene/sceneExperimentOperation";
	}
	@RequestMapping(value = "scheduleJobTest")
	public String scheduleJobTest(Model model,String courseId,String coursewareId,String instanceId)throws Exception {

		logger.debug("dingh===定时任务开始");
		SceneEnvironment sceneEnvironment=sceneEnvironmentService.get("440e6a65-39d3-11e7-a4bc-d8cb8a6cc1b8");
		SceneEnvironmentInstance sceneEnvironmentInstance=new SceneEnvironmentInstance();
		sceneEnvironmentInstance.setEnvironmentName("dingh==" + sceneEnvironment.getEnvironmentName());
		sceneEnvironmentInstanceService.save(sceneEnvironmentInstance);
		sceneEnvironmentService.instanceScheduleJob(sceneEnvironmentInstance, sceneEnvironment);

		return null;
	}
	/**
	 * 〈方法的功能描述〉:
	 *获取取消订单的定时任务的时间
	 * @Date	 2016/12/6/11:22
	 * @author  dingh
	 * @methodName
	 * @param
	 * @return
	 **/

	public String getCancelTime(Date orderTime,int afterMinute){
		Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
		ca.setTime(orderTime); //设置时间为当前时间
		ca.set(Calendar.MINUTE, ca.get(Calendar.MINUTE) + afterMinute);
		Date time = ca.getTime(); //结果
		int year = ca.get(Calendar.YEAR);//获取年份
		int month=ca.get(Calendar.MONTH)+1;//获取月份
		int day=ca.get(Calendar.DATE);//获取日
		int hour=ca.get(Calendar.HOUR_OF_DAY);//小时
		int minute=ca.get(Calendar.MINUTE);//分
		int second=ca.get(Calendar.SECOND);//秒
		//"30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务
		String cancelTime=second+" "+minute+" "+hour+" "+day+" "+month+" "+"? "+year;
		return cancelTime;

	}
	public Date getCancelTimeByDate(Date orderTime,int afterMinute){
		Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
		ca.setTime(orderTime); //设置时间为当前时间
		ca.set(Calendar.MINUTE, ca.get(Calendar.MINUTE) + afterMinute);
//		ca.add(Calendar.SECOND, +30);
		Date time = ca.getTime(); //结果
		return time;
	}

	@RequestMapping(value ="toSceneEnvironmentTopology")
	public String toSceneEnvironmentTopology(SceneEnvironment sceneEnvironment, HttpServletRequest request, HttpServletResponse response, Model model) {

		String topologyXml="<?xml version=\"1.0\" encoding=\"utf8\"?><modes><mode classname=\"module\" class=\"module\" attr_prop_attri1=\"\" attr_prop_attri2=\"\" attr_prop_attri3=\"1=0&amp;&amp;IPv4,0=1&amp;&amp;IPv6\" id=\"18\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/baseMode3.png\" top=\"192\" left=\"189\" zIndex=\"18\" width=\"50\" height=\"50\"/><mode classname=\"module\" class=\"module\" attr_prop_attri1=\"\" attr_prop_attri2=\"1=6786b89eb4664ed496b15834dbc7cb7d&amp;&amp;cirros\" attr_prop_attri3=\"1=0&amp;&amp;IPv4,0=1&amp;&amp;IPv6\" attr_prop_attri4=\"\" id=\"12\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/baseMode4.png\" top=\"189\" left=\"817\" zIndex=\"12\" width=\"50\" height=\"50\"/><line strokeweight=\"2\" strokecolor=\"black\" brokenType=\"1\" stroke=\"purple\" path=\"M 189 192 L 843 241 \" d=\"M 189 192 L 843 241 \" id=\"line12\" style=\"cursor: pointer; fill: none; stroke: black; stroke-width: 2;\" marker-end=\"url(#arrowa)\" wBaseMode=\"module12\" wIndex=\"7\" xBaseMode=\"module18\" xIndex=\"0\" attr_prop_attri1=\"2b\" attr_prop_attri2=\"3b\" attr_prop_attri3=\"4s\" attr_prop_attri4=\"5s\"/></modes>";
		topologyXml=StringEscapeUtils.unescapeXml(topologyXml);
		String str = topologyXml.replace("\"", "\\\"");

		model.addAttribute("topologyXml",str);

		return "modules/scene/sceneEnvironmentTopology";
	}
	@ResponseBody
	@RequestMapping(value = "topologyShow")
	public String topologyShow(String topologyXml) {

		logger.debug("dingh===topologyXml");
		logger.debug("dingh===topologyXml="+topologyXml);
		topologyXml=StringEscapeUtils.unescapeXml(topologyXml);
		String str = topologyXml.replace("\"", "\\\"");
//<?xml version="1.0" encoding="utf8"?><modes><mode classname="module" class="module" attr_prop_attri1="vm1" attr_prop_attri2="1=6786b89eb4664ed496b15834dbc7cb7d&amp;&amp;cirros" attr_prop_attri3="0=0&amp;&amp;IPv4,1=1&amp;&amp;IPv6" attr_prop_attri4="10.0.0.12" id="18" title="" backImgSrc="http://localhost:8080/static/images/topology/baseMode4.png" top="130" left="789" zIndex="18" width="50" height="50"/><mode classname="module" class="module" attr_prop_attri1="switch1" attr_prop_attri2="10.0.0.0/24" attr_prop_attri3="1=0&amp;&amp;IPv4,0=1&amp;&amp;IPv6" id="12" title="" backImgSrc="http://localhost:8080/static/images/topology/baseMode3.png" top="222" left="289" zIndex="12" width="50" height="50"/><line strokeweight="2" strokecolor="black" brokenType="1" stroke="purple" path="M 341 248 L 815 130 " d="M 341 248 L 815 130 " id="line14" style="cursor: pointer; fill: none; stroke: black; stroke-width: 2;" marker-end="url(#arrowa)" xBaseMode="module12" xIndex="5" wBaseMode="module18" wIndex="2" attr_prop_attri1="2b" attr_prop_attri2="3b" attr_prop_attri3="4s" attr_prop_attri4="5s"/></modes>
//<?xml version='1.0' encoding='utf8'?><modes><mode classname="module" class="module" attr_prop_attri1="{&quot;title&quot;:&quot;标题1&quot;,&quot;type&quot;:&quot;select&quot;,&quot;data&quot;:[{&quot;desc&quot;:&quot;教师&quot;,&quot;value&quot;:&quot;0&quot;},{&quot;desc&quot;:&quot;学生&quot;,&quot;value&quot;:&quot;1&quot;}],&quot;selected&quot;:&quot;0&quot;}" attr_prop_attri2="{&quot;title&quot;:&quot;标题2&quot;,&quot;type&quot;:&quot;select&quot;,&quot;data&quot;:[{&quot;desc&quot;:&quot;教师&quot;,&quot;value&quot;:&quot;0&quot;},{&quot;desc&quot;:&quot;学生&quot;,&quot;value&quot;:&quot;1&quot;}],&quot;selected&quot;:&quot;1&quot;}" attr_prop_attri3="{&quot;title&quot;:&quot;标题3&quot;,&quot;type&quot;:&quot;input&quot;,&quot;data&quot;:&quot;3&quot;}" attr_prop_attri4="{&quot;title&quot;:&quot;标题4&quot;,&quot;type&quot;:&quot;input&quot;,&quot;data&quot;:&quot;4&quot;}" id="16" title="" backImgSrc="http://localhost:8080/static/images/topology/baseMode4.png" top="147" left="815" zIndex="16" width="50" height="50"/><mode classname="module" class="module" attr_prop_attri1="{&quot;title&quot;:&quot;标题1&quot;,&quot;type&quot;:&quot;select&quot;,&quot;data&quot;:[{&quot;desc&quot;:&quot;教师&quot;,&quot;value&quot;:&quot;0&quot;},{&quot;desc&quot;:&quot;学生&quot;,&quot;value&quot;:&quot;1&quot;}],&quot;selected&quot;:&quot;1&quot;}" attr_prop_attri2="{&quot;title&quot;:&quot;标题2&quot;,&quot;type&quot;:&quot;select&quot;,&quot;data&quot;:[{&quot;desc&quot;:&quot;教师&quot;,&quot;value&quot;:&quot;0&quot;},{&quot;desc&quot;:&quot;学生&quot;,&quot;value&quot;:&quot;1&quot;}],&quot;selected&quot;:&quot;0&quot;}" attr_prop_attri3="{&quot;title&quot;:&quot;标题3&quot;,&quot;type&quot;:&quot;input&quot;,&quot;data&quot;:&quot;1&quot;}" attr_prop_attri4="{&quot;title&quot;:&quot;标题4&quot;,&quot;type&quot;:&quot;input&quot;,&quot;data&quot;:&quot;2&quot;}" id="14" title="" backImgSrc="http://localhost:8080/static/images/topology/baseMode3.png" top="139" left="312" zIndex="14" width="50" height="50"/><line strokeweight="2" strokecolor="black" brokenType="1" stroke="purple" path="M 364 165 L 815 173" d="M 364 165 L 815 173" id="line12" style="cursor: pointer; fill: none; stroke: black; stroke-width: 2;" marker-end="url(#arrowa)" xBaseMode="module14" xIndex="5" wBaseMode="module16" wIndex="4" attr_prop_attri1="2" attr_prop_attri2="3" attr_prop_attri3="4" attr_prop_attri4="5"/></modes>
		logger.debug("dingh===topologyXml="+topologyXml);
		logger.debug("dingh===str="+str);
		//String str="<?xml version='1.0' encoding='utf8'?><modes><mode classname=\\\"module\\\" class=\\\"module\\\" attr_prop_attri1=\\\"{&quot;title&quot;:&quot;交换机名称&quot;,&quot;type&quot;:&quot;input&quot;,&quot;data&quot;:&quot;&quot;}\\\" attr_prop_attri2=\\\"{&quot;title&quot;:&quot;网络地址&quot;,&quot;type&quot;:&quot;input&quot;,&quot;data&quot;:&quot;&quot;}\\\" attr_prop_attri3=\\\"{&quot;title&quot;:&quot;IP版本&quot;,&quot;type&quot;:&quot;select&quot;,&quot;data&quot;:[{&quot;desc&quot;:&quot;IPv4&quot;,&quot;value&quot;:&quot;0&quot;},{&quot;desc&quot;:&quot;IPv6&quot;,&quot;value&quot;:&quot;1&quot;}]}\\\" id=\\\"12\\\" title=\\\"\\\" backImgSrc=\\\"http://localhost:8080/static/images/topology/baseMode3.png\\\" top=\\\"130\\\" left=\\\"370\\\" zIndex=\\\"12\\\" width=\\\"50\\\" height=\\\"50\\\"/></modes>";
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(topologyXml.getBytes("gbk"));
			Reader read = new InputStreamReader(is,"gbk");
			SAXReader reader = new SAXReader();
			Document document = reader.read(read);
			Element root = document.getRootElement();
			List<Element> childElements = root.elements();
			for (Element child : childElements) {
				logger.debug("child:" + child.getName()+" "+child.getText());
				if(child.getName().equals("mode"))
				{

					Mode mode=new Mode();
					mode.setId(child.attributeValue("id"));
					mode.setChild(child.attributeValue("child"));
					mode.setBackImgSrc(child.attributeValue("backImgSrc"));
					mode.setTitle(child.attributeValue("title"));
					mode.setTop(child.attributeValue("top"));

					mode.setLeft(child.attributeValue("left"));
					mode.setWidth(child.attributeValue("width"));
					mode.setHeight(child.attributeValue("height"));
					String attri=child.attributeValue("attr_prop_attri1");
					String attri3=child.attributeValue("attr_prop_attri3");

					//{"title":"标题1","type":"select","data":[{"desc":"教师","value":"0"},{"desc":"学生","value":"1"}],"selected":"1"}
					logger.debug("dingh===attri:" + attri);
					logger.debug("dingh===attri:" + attri3);
					if(child.attributeValue("backImgSrc").contains("baseMode3.png"))
					{
						String attri1=child.attributeValue("attr_prop_attri1");
						String attri13=child.attributeValue("attr_prop_attri3");

						//{"title":"标题1","type":"select","data":[{"desc":"教师","value":"0"},{"desc":"学生","value":"1"}],"selected":"1"}
						logger.debug("dingh===attri:" + attri);
						logger.debug("dingh===attri:" + attri3);
						//dingh===attri:{"title":"标题1","type":"select","data":[{"desc":"教师","value":"0"},{"desc":"学生","value":"1"}],"selected":"1"}
						//dingh===attri:{"title":"标题3","type":"input","data":"3"}
						mode.setType(1);


					}

				}
			}
		}catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}


		return null;
	}
}




